java - setText(String t) of JTextField cannot change the content of the textfield -


the picture show problem setting dialog. dialog's first jtextfield works fine settext(string t) method when change first jcombobox selected item, second jtextfield's settext(string t) method doesn't change content of itself's when change second jcombobox seleted item.

the source's code attached below. 2 jtextfield construct in same way. don't know why second one's settext(string) method doesn't work.

package ztio.gui.updategui;  import java.awt.container; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.ioexception; import java.util.arraylist; import java.util.arrays; import java.util.list; import java.util.map; import java.util.set;  import javax.swing.borderfactory; import javax.swing.box; import javax.swing.boxlayout; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jdialog; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.border.titledborder;    public class simplesettingdialog extends jdialog {     private final int max_thres_num = 10;     private final int max_value_thres_num = 10;     private jpanel dialogpane;     private jcombobox testcasecombo;     private jcombobox testcaseitemcombo;     private jcombobox[] judgeoperationcomboarray = new jcombobox[max_thres_num];     private jcombobox[] threstypecomboarray = new jcombobox[max_thres_num];     private string[] judgeoperationstrings;     private string[] threstypestrings;     private list<jbutton> testbtnlist;     private jtextfield showtargetchoice;     private jtextfield showtestcase;       public simplesettingdialog(jframe parent, boolean ismodal) {         // todo auto-generated constructor stub         super(parent, ismodal);         string[] judges = {">", "<", "="};         string[] types = {"int", "float", "string"};         judgeoperationstrings = judges;         threstypestrings = types;           dialogpane = (jpanel) getcontentpane();         dialogpane.setlayout(new gridbaglayout());         titledborder titled = borderfactory.createtitledborder("config thresholds");         dialogpane.setborder(titled);          jpanel testcasepanel = createtestcasepanel();          gridbagconstraints c = new gridbagconstraints();          c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 1.0;         c.weighty = 0.0;         c.gridwidth = 1;         c.gridx = 0;         c.gridy = 0;         dialogpane.add(testcasepanel, c);         c.fill = gridbagconstraints.both;         c.ipady = 0;         c.weightx = 1.0;         c.weighty = 1.0;         c.gridwidth = 1;         c.gridx = 0;         c.gridy = 1;          jpanel testitempanel = createtestcaseitempanel();          jpanel debug = new jpanel();         dialogpane.add(debug, c);     }      private jpanel createthreslistpanel() {         jpanel threslistpanel = new jpanel();         threslistpanel.setlayout(new boxlayout(threslistpanel, boxlayout.y_axis));         for(int = 0; < 2; ++i){             jpanel threspanel = createthrespanel(i);             threslistpanel.add(threspanel);         }         return threslistpanel;     }      private jpanel createthrespanel(int i) {         // todo auto-generated method stub         jpanel threspanel = new jpanel(new gridbaglayout());         titledborder titled = borderfactory.createtitledborder("the " + (((i+1)==1)?"1st":((i+1)==2?"2nd":((i+1==3?"3rd":((i+1)+"th"))))) + " threshold" );         threspanel.setborder(titled);         jcombobox threstypecombo = new jcombobox(threstypestrings);         threstypecomboarray[i] = threstypecombo;         threstypecombo.setselecteditem("string");         jcombobox judgeoperationcombo = new jcombobox(judgeoperationstrings);         judgeoperationcomboarray[i] = judgeoperationcombo;         judgeoperationcombo.setselecteditem("=");         jlabel label1 = new jlabel("threshold's type:");         jlabel label2 = new jlabel("judge operation:");          gridbagconstraints c = new gridbagconstraints();         c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 0.0;         c.gridwidth = 1;         c.gridx = 0;         c.gridy = 0;         threspanel.add(label1, c);          c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 1.0;         c.gridwidth = 1;         c.gridx = 1;         c.gridy = 0;         threspanel.add(threstypecombo, c);          c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 0.0;         c.gridwidth = 1;         c.gridx = 0;         c.gridy = 1;         threspanel.add(label2, c);          c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 1.0;         c.gridwidth = 1;         c.gridx = 1;         c.gridy = 1;         threspanel.add(judgeoperationcombo, c);          return threspanel;     }      private jpanel createtestcaseitempanel() {         // todo auto-generated method stub         string[] testitemnamearray = {"volume", "brightness"};         testcaseitemcombo = new jcombobox(testitemnamearray);          testcaseitemcombo.addactionlistener(new actionlistener() {              public void actionperformed(actionevent e) {                 // todo auto-generated method stub                 jcombobox cb = (jcombobox)e.getsource();                 string caseitemname = (string)cb.getselecteditem();                 string casename = (string)testcasecombo.getselecteditem();                 string targetstring = casename + "->" + caseitemname;                 system.out.println(targetstring);                 system.out.println("target text field: " + showtargetchoice.gettext());                 showtargetchoice.settext(targetstring);             }         });          titledborder titled;          titled = borderfactory.createtitledborder("test case item");         jpanel comp = new jpanel(new gridbaglayout());         comp.setborder(titled);          jbutton btnaddtestcaseitem = new jbutton("add");         btnaddtestcaseitem.addactionlistener(new actionlistener() {              public void actionperformed(actionevent e) {                 // todo auto-generated method stub              }         });          jbutton btndeltestcaseitem = new jbutton("del");         btndeltestcaseitem.addactionlistener(new actionlistener() {              public void actionperformed(actionevent e) {                 // todo auto-generated method stub              }         });             gridbagconstraints c = new gridbagconstraints();          c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 0.0;         c.gridwidth = 1;         c.gridx = 0;         c.gridy = 0;         comp.add(testcaseitemcombo, c);          c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 0.0;         c.gridwidth = 1;         c.gridx = 1;         c.gridy = 0;         comp.add(btnaddtestcaseitem, c);          c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 0.0;         c.gridwidth = 1;         c.gridx = 2;         c.gridy = 0;         comp.add(btndeltestcaseitem, c);          c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 1.0;         c.gridwidth = 1;         c.gridx = 3;         c.gridy = 0;         comp.add(new jpanel(), c);          //row 2         jlabel labelregexp = new jlabel("target string:");         showtargetchoice = new jtextfield();         c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 0.0;         c.gridwidth = 1;         c.gridx = 0;         c.gridy = 1;         comp.add(labelregexp, c);          c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 0.0;         c.gridwidth = 3;         c.gridx = 1;         c.gridy = 1;         comp.add(showtargetchoice, c);          jpanel threslistpanel = createthreslistpanel();         c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 0.0;         c.gridwidth = 4;         c.gridx = 0;         c.gridy = 2;         comp.add(threslistpanel, c);          return comp;     }      private jpanel createtestcasepanel() {         // todo auto-generated method stub         string[] testcasearray = {"light", "voice"};         testcasecombo = new jcombobox(testcasearray);         testcasecombo.addactionlistener(new actionlistener() {             public void actionperformed(actionevent e) {                 // todo auto-generated method stub                 jcombobox cb = (jcombobox)e.getsource();                 string casename = (string)cb.getselecteditem();                 system.out.println(casename);                 showtestcase.settext(casename);             }         });         titledborder titled;          titled = borderfactory.createtitledborder("test case");         jpanel comp = new jpanel(new gridbaglayout());         comp.setborder(titled);          jbutton btnaddtestcase = new jbutton("add");         btnaddtestcase.addactionlistener(new actionlistener() {              public void actionperformed(actionevent e) {                 // todo auto-generated method stub             }         });          jbutton btndeltestcase = new jbutton("del");         btndeltestcase.addactionlistener(new actionlistener() {              public void actionperformed(actionevent e) {                 // todo auto-generated method stub              }         });           gridbagconstraints c = new gridbagconstraints();          c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 0.0;         c.gridwidth = 1;         c.gridx = 0;         c.gridy = 0;         comp.add(testcasecombo, c);          c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 0.0;         c.gridwidth = 1;         c.gridx = 1;         c.gridy = 0;         comp.add(btnaddtestcase, c);          c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 0.0;         c.gridwidth = 1;         c.gridx = 2;         c.gridy = 0;         comp.add(btndeltestcase, c);          c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 1.0;         c.gridwidth = 1;         c.gridx = 3;         c.gridy = 0;         comp.add(new jpanel(), c);          jlabel label = new jlabel("selected test case:");         showtestcase = new jtextfield();         c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 0.0;         c.gridwidth = 1;         c.gridx = 0;         c.gridy = 1;         comp.add(label, c);          c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 1.0;         c.gridwidth = 3;         c.gridx = 1;         c.gridy = 1;         comp.add(showtestcase, c);          jpanel testcaseitempanel = createtestcaseitempanel();         c.fill = gridbagconstraints.horizontal;         c.ipady = 0;         c.weightx = 0.0;         c.gridwidth = 4;         c.gridx = 0;         c.gridy = 2;         comp.add(testcaseitempanel, c);            return comp;     }      private static void createandshowgui() {         jframe frame = new jframe("simplesettingdemo");         frame.setdefaultcloseoperation(jframe.exit_on_close);         jbutton btn = new jbutton("open setting dialog");         btn.addactionlistener(new actionlistener() {              @override             public void actionperformed(actionevent e) {                 // todo auto-generated method stub                 simplesettingdialog simplesettingdialog = new simplesettingdialog(frame, true);                 simplesettingdialog.pack();                 simplesettingdialog.setvisible(true);             }         });         frame.getcontentpane().add(btn);         frame.pack();         frame.setvisible(true);     }      public static void main(string[] args) {         try {             uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());         } catch (classnotfoundexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (instantiationexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (illegalaccessexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (unsupportedlookandfeelexception e) {             // todo auto-generated catch block             e.printstacktrace();         }          javax.swing.swingutilities.invokelater(new runnable() {             public void run() {                 createandshowgui();             }         });      } } 

createtestcaseitempanel() executed twice. once executed createtestcasepanel(). second time @ line in constructor:

jpanel testitempanel = createtestcaseitempanel(); 

this second call creates local variable testitempanel not used. overrides of members, 1 of them showtargetchoice, points field not visible.

to fix issue rid of second call.


Comments