java - How to generate dynamically many JTextfield/JButton/... with different id using a loop? -
i'm working right on project java allowing manager create todo list , employee view different todo, process , modify in exemple adding comment.
the employee's interface should consist of:
- a lot of jtabbedpane dynamically generated depending on number of todo in database.
- each jtabbedpane must contain information of todo ( title, jtextfield add comment, jtextbox mark todo done , jbutton save our changes.
my problem when generate jtextfield, etc.. in different jtabbedpane have same id because use while loop follows:
private void init_employee() { try { /* create frame */ settitle("suptodo employee"); setsize(800, 800); /* new panel*/ jpanel toppanel = new jpanel(); toppanel.setlayout( new borderlayout() ); getcontentpane().add(toppanel); tabbedpane = new jtabbedpane(); /* select in data base */ statement stmt = (statement) connectionmanager.getconnection().createstatement(); string sql="select id, title, contenu, comments sup_todo done = 0"; resultset rset=(resultset) stmt.executequery(sql); while(rset.next()) { /* data */ todo todo = new todo(); todo.setid(rset.getlong(1)); todo.settitle(rset.getstring(2)); todo.setcontenu(rset.getstring(3)); todo.setcomments(rset.getstring(4)); /* create field */ panel = new jpanel(); panel.setlayout(null); title = new jlabel(todo.gettitle()); title.setbounds( 200, 0, 100, 100 ); contenu = new jlabel(todo.getcontenu()); contenu.setbounds(200, 50, 400, 200 ); employeetextfieldtodo = new jtextfield(); employeetextfieldtodo.setbounds(200,300,400,200); employeecheckboxtodo = new jcheckbox("mark done !"); employeecheckboxtodo.setbounds(200, 500, 200, 100); employeeaddcommenttodo = new jbutton("save"); employeeaddcommenttodo.setbounds(350, 600, 100, 50); //employeeaddcommenttodo.setbounds(x, y, width, height); /* action */ /* save comments */ employeeaddcommenttodo.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { /* problem here : text of last jtextfield, not of jtextfield in jtabbedpane */ string comment = employeetextfieldtodo.gettext(); joptionpane.showconfirmdialog(suptodoemployeeframe.this, "votre commentaire est : " + comment, "comment message", joptionpane.default_option); } }); /* mark done */ employeecheckboxtodo.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { } }); /* add panel */ panel.add(title); panel.add(contenu); panel.add(employeetextfieldtodo); panel.add(employeeaddcommenttodo); panel.add(employeecheckboxtodo); panel.setvisible(true); tabbedpane.addtab(todo.gettitle(), panel); toppanel.add(tabbedpane, borderlayout.center); } } catch (sqlexception e1) { e1.printstacktrace(); } }
how can generate jtexfield, jbutton, ... unique name action on save button , on checkbox concern right todo, execute sql command on right row , not last field name.
consider creating ui variables local in scope while
method. if need reference them within anonymous class (eg actionlistener
implementation), mark them final (be sure remove other variable declarations). example
final jtextfield employeetextfieldtodo = new jtextfield(); final jbutton employeeaddcommenttodo = new jbutton("save"); ... employeeaddcommenttodo.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { string comment = employeetextfieldtodo.gettext(); joptionpane.showconfirmdialog(suptodoemployeeframe.this, "votre commentaire est : " + comment, "comment message", joptionpane.default_option); } });
Comments
Post a Comment