java - Modify Listener on Text Field -
i have added modify listener on text field , when seek open dialog fires modify listener event , gives null pointer exception before dialog opened
here code
usernamedialog dialog = new usernamedialog(platformui.getworkbench().getactiveworkbenchwindow().getshell(), true); dialog.init("", usersession.hasadminrights(), false, true, false, ""); dialog.open(); if (dialog.getreturncode() == window.cancel) { return; }
dialog class
public class usernamedialog extends titleareadialog { private text txtusername; @override protected command createdialogarea(composite parent) { setmessage("enter user info , press ok"); settitle("user information"); composite area = (composite) super.createdialogarea(parent); composite container = new composite(area, swt.none); container.setlayout(new gridlayout(2, false)); container.setlayoutdata(new griddata(swt.fill, swt.fill, true, true, 1, 1)); label lblusername = new label(container, swt.none); lblusername.settext("user name"); txtusername = new text(container, swt.border); txtusername.setlayoutdata(new griddata(swt.fill, swt.center, true, false, 1, 1)); txtusername.addlistener(swt.modify, new listener() { @override public void handleevent(event event) { getbutton(idialogconstants.ok_id).setenabled(isvalidusername()); // seek open dialog handler comes here , throws exception on // getbutton(idialogconstants.ok_id) } }); if(newuser || currentuserisadmin) txtusername.seteditable(true); else txtusername.seteditable(false); txtusername.settext(name); new label(container, swt.none); new label(container, swt.none); homecoming area; } /** * create contents of button bar. * @param parent */ @override protected void createbuttonsforbuttonbar(composite parent) { okbutton = createbutton(parent, idialogconstants.ok_id, idialogconstants.ok_label, true); createbutton(parent, idialogconstants.cancel_id, idialogconstants.cancel_label, false); okbutton.setenabled(false); } private boolean isvalidusername() { string username = txtusername.gettext(); homecoming username != null && username.trim().length() > 0; } }
it within dialog called before creation of dialog think because okbutton null
ok, here illustration dialog enable ok button if username @ to the lowest degree 1 character long:
private text text; protected usernamedialog(shell parentshell) { super(parentshell); } @override protected void configureshell(shell newshell) { super.configureshell(newshell); newshell.settext("dialog"); } @override protected command createdialogarea(composite parent) { composite container = (composite) super.createdialogarea(parent); text = new text(container, swt.border); text.setlayoutdata(new griddata(swt.beginning, swt.center, false, false)); text.addlistener(swt.modify, new listener() { @override public void handleevent(event event) { getbutton(idialogconstants.ok_id).setenabled(isvalidusername()); } }); homecoming container; } private boolean isvalidusername() { string username = text.gettext(); homecoming username != null && username.trim().length() > 0; } @override protected void okpressed() { if (isvalidusername()) super.okpressed(); else system.out.println("invalid username"); } @override protected void createbuttonsforbuttonbar(composite parent) { createbutton(parent, idialogconstants.ok_id, idialogconstants.ok_label, true); createbutton(parent, idialogconstants.cancel_id, idialogconstants.cancel_label, false); getbutton(idialogconstants.ok_id).setenabled(false); } public static void main(string[] args) { new usernamedialog(new shell()).open(); }
java radio-button swt selection
No comments:
Post a Comment