java - How to Access JDialog information from parent JFrame? -
** have updated question per , comments received. question is: after calling following code:
searchbox searchbox = new searchbox(); // searchbox extends jdialog int state = searchbox.showsearchdialog(); // sets visible true , returns state
i want let user enter input in searchbox (jdialog). "halt" jframe class when searchbox.showsearchdialog()
called (shouldn't already?? not after setvisible(true)
called). , once user presses ok in jdialog (searchbox class) "resume" jframe class , call string query = searchbox.getquery();
.
i thought sine setvisible(true) called searchbox.showsearchdialog() method should halt not. code runs through from:
seachbox = new searchbox(); int state = seachbox.showseachdialog(); string query = searchbox.getquery();
without stopping. query string coming wrong because user never had chance input before code calls method.
jframe
class:
import java.awt.cursor;
@suppresswarnings("serial") public class datapersistance extends jframe {
private jpanel contentpane; private jtable table; private datapersistancecontroller controller; private databaseconnector dbconnector; /** * launch application. */ public static void main(string[] args) { // sets , feel windows try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception e1) { e1.printstacktrace(); } catch (instantiationexception e1) { e1.printstacktrace(); } catch (illegalaccessexception e1) { e1.printstacktrace(); } catch (unsupportedlookandfeelexception e1) { e1.printstacktrace(); } eventqueue.invokelater(new runnable() { public void run() { try { datapersistance frame = new datapersistance(); frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create frame. * * @throws sqlexception */ public datapersistance() throws sqlexception { this.controller = new datapersistancecontroller(); this.dbconnector = new databaseconnector(); settitle("data persistance tool - enterra solutions, llc."); setdefaultcloseoperation(jframe.exit_on_close); setbounds(100, 100, 1000, 750); jmenubar menubar = new jmenubar(); setjmenubar(menubar); jmenu mnfile = new jmenu("file"); mnfile.setmnemonic(keyevent.vk_f); menubar.add(mnfile); jmenuitem mntmconnect = new jmenuitem("connect"); mnfile.add(mntmconnect); jmenuitem mntmexit = new jmenuitem("exit"); mntmexit.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { dispose(); } }); mnfile.add(mntmexit); jmenu mnedit = new jmenu("edit"); mnedit.setmnemonic(keyevent.vk_e); menubar.add(mnedit); jmenuitem mntmcopyresults = new jmenuitem("copy results"); mnedit.add(mntmcopyresults); jmenu mntools = new jmenu("tools"); mntools.setmnemonic(keyevent.vk_t); menubar.add(mntools); jmenuitem mntmkbload = new jmenuitem("auto load (from kb)"); mntools.add(mntmkbload); jmenuitem mntmmanualloadke = new jmenuitem("manual load (.ke file)"); mntmmanualloadke.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { jfilechooser fchoose = new jfilechooser(); fchoose.setfilefilter(new filefilter() { @override public boolean accept(file f) { return (f.isfile() && (f.getname().tolowercase().endswith(".txt") || f .getname().tolowercase() .endswith(".ke")) || f.isdirectory()); } @override public string getdescription() { return null; } }); // showopendialog returns value. did not use. fchoose.showopendialog(contentpane); fchoose.setvisible(true); if (fchoose.getselectedfile() != null) { try { setcursor(cursor .getpredefinedcursor(cursor.wait_cursor)); controller.manuallyloadrecipes(fchoose .getselectedfile()); } catch (filenotfoundexception e1) { setcursor(cursor .getpredefinedcursor(cursor.default_cursor)); joptionpane .showmessagedialog( contentpane, "there problem loading file database.", "load error", joptionpane.error_message); system.out.println("file not found"); return; } catch (sqlexception e1) { setcursor(cursor .getpredefinedcursor(cursor.default_cursor)); joptionpane .showmessagedialog( contentpane, "there problem loading file database.", "load error", joptionpane.error_message); system.out.println("sql error"); return; } setcursor(cursor.getpredefinedcursor(cursor.default_cursor)); joptionpane.showmessagedialog(contentpane, "load successful."); } } }); mntools.add(mntmmanualloadke); jmenuitem mntmgenerateketext = new jmenuitem("generate ke text"); mntools.add(mntmgenerateketext); jmenuitem mntmdeleterecipies = new jmenuitem("delete recipies"); mntools.add(mntmdeleterecipies); jmenu mnsettings = new jmenu("settings"); mnsettings.setmnemonic(keyevent.vk_s); menubar.add(mnsettings); jmenuitem mntmview = new jmenuitem("view"); mnsettings.add(mntmview); jmenuitem mntmconnectionpreferences = new jmenuitem( "connection preferences"); mnsettings.add(mntmconnectionpreferences); jmenu mnhelp = new jmenu("help"); mnhelp.setmnemonic(keyevent.vk_h); menubar.add(mnhelp); jmenuitem mntmreadmefile = new jmenuitem("readme"); mnhelp.add(mntmreadmefile); jmenuitem mntmaboutdatapersistance = new jmenuitem("about"); mntmaboutdatapersistance.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { jdialog aboutwindow = new aboutdialog(); aboutwindow.setvisible(true); } }); mnhelp.add(mntmaboutdatapersistance); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); gridbaglayout gbl_contentpane = new gridbaglayout(); gbl_contentpane.columnwidths = new int[] { 36, 23, 82, 464, 243, 0, 0 }; gbl_contentpane.rowheights = new int[] { 0, 0, 0, 9, 127, 0, 0, 0, 0, 0 }; gbl_contentpane.columnweights = new double[] { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, double.min_value }; gbl_contentpane.rowweights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, double.min_value }; contentpane.setlayout(gbl_contentpane); jtoolbar toolbar = new jtoolbar(); toolbar.setfloatable(false); gridbagconstraints gbc_toolbar = new gridbagconstraints(); gbc_toolbar.anchor = gridbagconstraints.line_start; gbc_toolbar.gridwidth = 6; gbc_toolbar.insets = new insets(0, 0, 5, 0); gbc_toolbar.gridx = 0; gbc_toolbar.gridy = 0; contentpane.add(toolbar, gbc_toolbar); jbutton btnnewbutton = new jbutton("search"); btnnewbutton.sethorizontalalignment(swingconstants.left); btnnewbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { // todo when search button clicked searchbox searchbox = new searchbox(); int state = searchbox.showsearchdialog(); string query = searchbox.getquery(); if (state == searchbox.ok_state) { try { resultset results = dbconnector.executequery(query); defaulttablemodel tm = (defaulttablemodel) table .getmodel(); table.setmodel(dbutils.resultsettotablemodel(results)); tm.firetabledatachanged(); } catch (sqlexception e1) { e1.printstacktrace(); } } } }); toolbar.add(btnnewbutton); jseparator separator = new jseparator(); gridbagconstraints gbc_separator = new gridbagconstraints(); gbc_separator.fill = gridbagconstraints.horizontal; gbc_separator.gridwidth = 6; gbc_separator.insets = new insets(0, 0, 5, 5); gbc_separator.gridx = 0; gbc_separator.gridy = 1; contentpane.add(separator, gbc_separator); jlabel lblnewlabel = new jlabel("data persistance tool"); gridbagconstraints gbc_lblnewlabel = new gridbagconstraints(); gbc_lblnewlabel.gridwidth = 6; gbc_lblnewlabel.insets = new insets(0, 0, 5, 0); gbc_lblnewlabel.gridx = 0; gbc_lblnewlabel.gridy = 2; contentpane.add(lblnewlabel, gbc_lblnewlabel); lblnewlabel.setfont(new font("tahoma", font.bold, 35)); // ========= create jtable , populate wih model ========= table = new jtable(); table.setmodel(dbconnector.getallfromdatabasetablemodel()); table.addmouselistener(new mouseadapter() { public void mouseclicked(mouseevent e) { if (e.getclickcount() == 2) { jtable target = (jtable) e.getsource(); int row = target.getselectedrow(); int column = target.getselectedcolumn(); // action } } }); // table.setminimumsize(new dimension(200, 400)); // table.setpreferredsize(new dimension(200, 600)); table.setautocreaterowsorter(true); table.setfillsviewportheight(true); table.getcolumnmodel().getcolumn(0).setpreferredwidth(10); table.getcolumnmodel().getcolumn(1).setpreferredwidth(5); // table = new jtable(); gridbagconstraints gbc_table = new gridbagconstraints(); gbc_table.insets = new insets(0, 0, 5, 0); gbc_table.gridwidth = 6; gbc_table.gridheight = 4; gbc_table.fill = gridbagconstraints.both; gbc_table.gridx = 0; gbc_table.gridy = 4; // todo lines commented because of memory // insert table inside scrollpane jscrollpane scrollpane = new jscrollpane(table); // add table inside scrollpane contentpane.add(scrollpane, gbc_table); jlabel lblv = new jlabel("v1.0.0 - enterra solutions, llc."); gridbagconstraints gbc_lblv = new gridbagconstraints(); gbc_lblv.gridwidth = 6; gbc_lblv.gridx = 0; gbc_lblv.gridy = 8; contentpane.add(lblv, gbc_lblv); jbutton btnrefresh = new jbutton("refresh"); btnrefresh.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { try { defaulttablemodel tm = (defaulttablemodel) table.getmodel(); table.setmodel(dbconnector.getallfromdatabasetablemodel()); tm.firetabledatachanged(); } catch (sqlexception e1) { e1.printstacktrace(); } } }); toolbar.add(btnrefresh); } }
now, jdialogsearch
box comes up. here full class:
import java.awt.borderlayout; @suppresswarnings("serial") public class searchbox extends jdialog { private final jpanel contentpanel = new jpanel(); private jtextfield textfieldguid; private jtextfield textfieldtitle; private jtextfield textfieldcallsfor; private jtextfield textfieldcontains; public static final int ok_state = 0; public static final int cancel_state = 1; private int state = cancel_state; string query; /** * launch application. */ public static void main(string[] args) { try { searchbox dialog = new searchbox(); dialog.setdefaultcloseoperation(jdialog.dispose_on_close); dialog.setvisible(true); } catch (exception e) { e.printstacktrace(); } } /** * create dialog. */ public searchbox() { setbounds(100, 100, 350, 300); getcontentpane().setlayout(new borderlayout()); contentpanel.setborder(new emptyborder(5, 5, 5, 5)); getcontentpane().add(contentpanel, borderlayout.west); gridbaglayout gbl_contentpanel = new gridbaglayout(); gbl_contentpanel.columnwidths = new int[] { 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20 }; gbl_contentpanel.rowheights = new int[] { 34, 0, 0, 0, 0, 0, 0, 0, 20 }; gbl_contentpanel.columnweights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, double.min_value }; gbl_contentpanel.rowweights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, double.min_value }; contentpanel.setlayout(gbl_contentpanel); jlabel lblnewlabel = new jlabel("recipe search"); lblnewlabel.setfont(new font("tahoma", font.bold, 15)); gridbagconstraints gbc_lblnewlabel = new gridbagconstraints(); gbc_lblnewlabel.anchor = gridbagconstraints.north; gbc_lblnewlabel.gridwidth = 14; gbc_lblnewlabel.insets = new insets(0, 0, 5, 0); gbc_lblnewlabel.gridx = 0; gbc_lblnewlabel.gridy = 0; contentpanel.add(lblnewlabel, gbc_lblnewlabel); final jcheckbox chckbxguid = new jcheckbox("guid"); gridbagconstraints gbc_chckbxguid = new gridbagconstraints(); gbc_chckbxguid.anchor = gridbagconstraints.west; gbc_chckbxguid.insets = new insets(0, 0, 5, 5); gbc_chckbxguid.gridx = 0; gbc_chckbxguid.gridy = 1; contentpanel.add(chckbxguid, gbc_chckbxguid); textfieldguid = new jtextfield(); gridbagconstraints gbc_textfield = new gridbagconstraints(); gbc_textfield.gridwidth = 11; gbc_textfield.insets = new insets(0, 0, 5, 5); gbc_textfield.fill = gridbagconstraints.horizontal; gbc_textfield.gridx = 2; gbc_textfield.gridy = 1; contentpanel.add(textfieldguid, gbc_textfield); textfieldguid.setcolumns(10); final jcheckbox chckbxtitle = new jcheckbox("title"); gridbagconstraints gbc_chckbxtitle = new gridbagconstraints(); gbc_chckbxtitle.anchor = gridbagconstraints.west; gbc_chckbxtitle.insets = new insets(0, 0, 5, 5); gbc_chckbxtitle.gridx = 0; gbc_chckbxtitle.gridy = 2; contentpanel.add(chckbxtitle, gbc_chckbxtitle); textfieldtitle = new jtextfield(); gridbagconstraints gbc_textfield_1 = new gridbagconstraints(); gbc_textfield_1.gridwidth = 11; gbc_textfield_1.insets = new insets(0, 0, 5, 5); gbc_textfield_1.fill = gridbagconstraints.horizontal; gbc_textfield_1.gridx = 2; gbc_textfield_1.gridy = 2; contentpanel.add(textfieldtitle, gbc_textfield_1); textfieldtitle.setcolumns(10); final jcheckbox chckbxcallsfor = new jcheckbox("calls for"); gridbagconstraints gbc_chckbxcallsfor = new gridbagconstraints(); gbc_chckbxcallsfor.anchor = gridbagconstraints.west; gbc_chckbxcallsfor.insets = new insets(0, 0, 5, 5); gbc_chckbxcallsfor.gridx = 0; gbc_chckbxcallsfor.gridy = 3; contentpanel.add(chckbxcallsfor, gbc_chckbxcallsfor); textfieldcallsfor = new jtextfield(); gridbagconstraints gbc_textfield_2 = new gridbagconstraints(); gbc_textfield_2.gridwidth = 11; gbc_textfield_2.insets = new insets(0, 0, 5, 5); gbc_textfield_2.fill = gridbagconstraints.horizontal; gbc_textfield_2.gridx = 2; gbc_textfield_2.gridy = 3; contentpanel.add(textfieldcallsfor, gbc_textfield_2); textfieldcallsfor.setcolumns(10); final jcheckbox chckbxcontains = new jcheckbox("contains "); gridbagconstraints gbc_chckbxcontains = new gridbagconstraints(); gbc_chckbxcontains.insets = new insets(0, 0, 5, 5); gbc_chckbxcontains.gridx = 0; gbc_chckbxcontains.gridy = 4; contentpanel.add(chckbxcontains, gbc_chckbxcontains); textfieldcontains = new jtextfield(); gridbagconstraints gbc_textfield_3 = new gridbagconstraints(); gbc_textfield_3.gridwidth = 11; gbc_textfield_3.insets = new insets(0, 0, 5, 5); gbc_textfield_3.fill = gridbagconstraints.horizontal; gbc_textfield_3.gridx = 2; gbc_textfield_3.gridy = 4; contentpanel.add(textfieldcontains, gbc_textfield_3); textfieldcontains.setcolumns(10); jbutton btnnewbutton = new jbutton("search"); btnnewbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { // todo when search button clicked query = "select * `temp`.`kb_recipies`"; // add if needed if (chckbxguid.isselected() || chckbxtitle.isselected() || chckbxcallsfor.isselected() || chckbxcontains.isselected()) { query += " "; } // -------------------- if (chckbxguid.isselected()) { query += "fpitemguid=" + textfieldguid.gettext(); // add , if needed if (chckbxtitle.isselected() || chckbxcallsfor.isselected() || chckbxcontains.isselected()) { query += " , "; } } if (chckbxtitle.isselected()) { query += "recipetitle=" + textfieldtitle.gettext(); // add , if needed if (chckbxcallsfor.isselected() || chckbxcontains.isselected()) { query += " , "; } } // if (chckbxcallsfor.isselected()) // { // query += "fpitemguid=" + textfieldguid.gettext(); // // add , if needed // if (chckbxcontains.isselected()) // { // query += " , "; // } // } // if (chckbxcontains.isselected()) // { // query += "fpitemguid=" + textfieldguid.gettext(); // } query += ";"; state = ok_state; dispose(); } }); gridbagconstraints gbc_btnnewbutton = new gridbagconstraints(); gbc_btnnewbutton.insets = new insets(0, 0, 5, 5); gbc_btnnewbutton.gridwidth = 14; gbc_btnnewbutton.gridx = 0; gbc_btnnewbutton.gridy = 6; contentpanel.add(btnnewbutton, gbc_btnnewbutton); } public boolean isboxselected() { return true; } public string getquery() { return this.query; } public int showsearchdialog() { // other setup... setvisible(true); return state; } }
your dialog needs provide kind of getter caller can call state...
public class jdialogsearch ... { public static final int ok_state = 0; public static final int cancel_state = 1; // cause it's nice know use did // ie canceled, okay'ed, didn't find results, ever... private int state = cancel_state; private string searchresult; //..../// ok.addactionlistener( new actionlistener() { private class oklistener implements actionlistener { public void actionperformed(actionevent e) { searchresult = somesearchfield.gettext(); // or ever need state = ok_state; dispose(); // close window } } }); //...// public int showsearchdialog() { // other setup... setvisible(true); return state; } public string getsearchresult() { return searchresult; }
then in call code call showsearchdialog
, if user clicks ok
, search result...
jdialogseach dialog = new jdialogseach(); // seach box implementation of jdialog switch (dialog.showsearchdialog()) { case jdialogseach.ok_state: mysearchresult = dialog.getsearchresult(); break; } dialog.setvisible(true);
of course, simple use joptionpane
, depend on requirements.
check out how make dialogs more details
updated
in order make application execution halt while dialog visible, need make dialog modal.
in dialogs constructor, should calling setmodal(true)
, discussed @ length in link provided @ end of last section
Comments
Post a Comment