swing - GUI Issue with JTextField & RMI Java -
i'm having strange issue using tables rmi. client implementation of time slot booking system, i've implemented table.
i'm faced 2 problems. first 1 causing table update after change had been made. solution seemed
private void cleanup() { panel.removeall(); panel.setvisible(false); panel.revalidate(); showtable(); }
and appear working. (or maybe causing issue, i'm not sure)
the problem have jtextfield inside method calls actual booking.
private jtextfield txtclientname; txtclientname = new jtextfield(); txtclientname.settext("clientname");
and in confirm button listener -
callbookingslot(buttonaction, txtclientname.gettext());
the strange thing works initially, once. working mean putting correct value extracted jtextfield table. first time round put in value of user typed in field. subsequent goes , put in string "clientname"
anyone have ideas? issue doesn't appear rmi related, i've tried without rmi , value taken text field still behaves same way. know should looking @ firetableupdated etc, am, great if 1 of ones fixed.
edit - more info
import java.awt.eventqueue; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtable; import javax.swing.jtextfield; import javax.swing.listselectionmodel; public class stackoverflowgui { private static jframe frame; private static jtable table; private static jpanel panel; private jtextfield txtclientname; private static jframe bookingpopup = new jframe(); public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { stackoverflowgui window = new stackoverflowgui(); window.frame.setvisible(true); } }); } public stackoverflowgui() { initialize(); } private void initialize() { panel = new jpanel(); frame = new jframe(); frame.setbounds(100, 100, 700, 751); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().setlayout(null); panel.setbounds(10, 11, 674, 576); frame.getcontentpane().add(panel); showtable(); } private void showtable() { table = new jtable(); panel.add(table); panel.setvisible(true); table.setselectionmode(listselectionmodel.single_selection); table.setcellselectionenabled(true); showbookingpopup(2, 2); } private void showbookingpopup(int row, int col) { bookingpopup.setbounds(100, 100, 220, 185); bookingpopup.setdefaultcloseoperation(jframe.exit_on_close); bookingpopup.getcontentpane().setlayout(null); txtclientname = new jtextfield(); txtclientname.settext("clientname"); txtclientname.setbounds(10, 11, 184, 20); bookingpopup.getcontentpane().add(txtclientname); txtclientname.setcolumns(10); bookingpopup.setvisible(true); jpanel panel = new jpanel(); panel.setbounds(10, 65, 184, 33); bookingpopup.getcontentpane().add(panel); jbutton btnsubmit = new jbutton("submit"); btnsubmit.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { //here - works first time system.out.println(txtclientname.gettext()); //continues work if don't call cleanup - main window not update cleanup(); } }); btnsubmit.setbounds(10, 113, 89, 23); bookingpopup.getcontentpane().add(btnsubmit); } private void cleanup() { panel.removeall(); panel.setvisible(false); panel.revalidate(); showtable(); }
}
your posted code has several major problems including:
- use of static fields instance fields should used
- use of null layouts ,
setbounds(...)
, avoiding use of swing layout managers - use of multiple jframes should using jdialogs
- but importantly, re-creating variables , components unnecessarily. if need clear fields, clear fields, don't discard them.
for example:
import java.awt.borderlayout; import java.awt.event.actionevent; import javax.swing.*; import javax.swing.table.defaulttablemodel; public class dataintotable extends jpanel { private static final string[] col_names = {"first name", "lastname"}; private defaulttablemodel model = new defaulttablemodel(col_names, 0); private jtable table = new jtable(model); private jtextfield firstnamefield = new jtextfield(10); private jtextfield lastnamefield = new jtextfield(10); private jpanel dialogpanel = new jpanel(); public dataintotable(final jframe frame) { setlayout(new borderlayout()); add(new jscrollpane(table)); dialogpanel.add(new jlabel("first name:")); dialogpanel.add(firstnamefield); dialogpanel.add(new jlabel("second name:")); dialogpanel.add(lastnamefield); dialogpanel.add(new jbutton(new abstractaction("submit") { @override public void actionperformed(actionevent arg0) { string[] rowdata = {firstnamefield.gettext(), lastnamefield.gettext()}; model.addrow(rowdata); firstnamefield.settext(""); lastnamefield.settext(""); } })); jdialog dialog = new jdialog(frame, "enter name", false); dialog.getcontentpane().add(dialogpanel); dialog.pack(); dialog.setlocationrelativeto(null); dialog.setvisible(true); } private static void createandshowgui() { jframe frame = new jframe("data table"); dataintotable mainpanel = new dataintotable(frame); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(mainpanel); frame.pack(); frame.setlocationbyplatform(true); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } }
Comments
Post a Comment