swing - Java: SwingPropertyChangeSupport -


i trying make mvc java swing program makes use of swingpropertychangesupport notify view whenever model gets updated. problem having notifications not seem happening.

i have prepared sscce below. in sscce, there swing gui has button , text field. when click button, counter in model gets incremented, , view supposed notified can update itself. however, appears notifications not sent/received (i not sure -- both) though have checked make sure oldvalue , newvalue different each other. appreciate assistance in understanding i've gone wrong. thanks!

import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.beans.propertychangeevent; import java.beans.propertychangelistener;  import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.swingutilities; import javax.swing.event.swingpropertychangesupport;   public class main extends jframe {      public main() {         propertychangeview thegui = new propertychangeview();         setdefaultcloseoperation(jframe.exit_on_close);         setresizable(true);         add(thegui);         pack();         setvisible(true);     }      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             public void run() {                 new main();             }         });     } }   class propertychangeview extends jpanel {     private jbutton button;     private jtextfield textfield;     private gridbagconstraints gbc;     private propertychangecontroller controller;      public propertychangeview() {         super(new gridbaglayout());         controller = new propertychangecontroller();         button = new jbutton("click me increment count");         textfield = new jtextfield(10);          button.addactionlistener(new buttonlistener());         addpropertychangelistener(new mypropertychangelistener());          gbc = new gridbagconstraints();         gbc.gridheight = 1;         gbc.gridwidth = 1;         gbc.anchor = gridbagconstraints.line_start;         gbc.fill = gridbagconstraints.both;          gbc.gridx = 0;         gbc.gridy = 0;         add(button, gbc);          gbc.gridx = 1;         gbc.gridy = 0;         add(textfield, gbc);     }      private class mypropertychangelistener implements propertychangelistener {         @override         public void propertychange(propertychangeevent evt) {             system.out.println("event received " + evt);              if (evt.getpropertyname().equals(propertychangemodel.changed)) {                 textfield.settext(evt.getnewvalue().tostring());             }         }     }      private class buttonlistener implements actionlistener {         @override         public void actionperformed(actionevent e) {             controller.setcounter(controller.getcounter() + 1);             system.out.println("counter = " + controller.getcounter());         }     }  }   class propertychangecontroller {     private propertychangemodel model;      public propertychangecontroller() {         model = new propertychangemodel();     }      public int getcounter() {         return model.getcounter();     }      public void setcounter(int i) {         model.setcounter(i);     } }   class propertychangemodel {     public static final string changed = "property change model updated";      private int counter;     private swingpropertychangesupport pcs;      public propertychangemodel() {         counter = 0;         pcs = new swingpropertychangesupport(this);     }      public int getcounter() {         return counter;     }      public void setcounter(int i) {         int oldvalue = counter;         int newvalue = i;         counter = newvalue;         pcs.firepropertychange(changed, oldvalue, newvalue);         system.out.println("setcounter finished oldvalue=" + oldvalue + ", newvalue=" + newvalue);     } } 

i've not run program, see 1 thing out of order here:

public void setcounter(int i) {     int oldvalue = counter;     int newvalue = i;     pcs.firepropertychange(changed, oldvalue, newvalue);     counter = newvalue;     system.out.println("setcounter finished oldvalue=" + oldvalue + ", newvalue=" + newvalue); } 

which should be:

public void setcounter(int i) {     int oldvalue = counter;     int newvalue = i;     counter = newvalue;     pcs.firepropertychange(changed, oldvalue, newvalue);     system.out.println("setcounter finished oldvalue=" + oldvalue + ", newvalue=" + newvalue); } 

you should fire notification after updating model's value.

your main problem though add no propertychangelistener model.


e.g.

import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.beans.propertychangeevent; import java.beans.propertychangelistener;  import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.swingutilities; import javax.swing.event.swingpropertychangesupport;  public class main extends jframe {     public main() {       propertychangeview thegui = new propertychangeview();       setdefaultcloseoperation(jframe.exit_on_close);       setresizable(true);       add(thegui);       pack();       setvisible(true);    }     public static void main(string[] args) {       swingutilities.invokelater(new runnable() {          public void run() {             new main();          }       });    } }  class propertychangeview extends jpanel {    private jbutton button;    private jtextfield textfield;    private gridbagconstraints gbc;    private propertychangecontroller controller;     public propertychangeview() {       super(new gridbaglayout());       propertychangemodel model = new propertychangemodel();       controller = new propertychangecontroller(model);       button = new jbutton("click me increment count");       textfield = new jtextfield(10);        button.addactionlistener(new buttonlistener());       model.addpropertychangelistener(new mypropertychangelistener());         gbc = new gridbagconstraints();       gbc.gridheight = 1;       gbc.gridwidth = 1;       gbc.anchor = gridbagconstraints.line_start;       gbc.fill = gridbagconstraints.both;        gbc.gridx = 0;       gbc.gridy = 0;       add(button, gbc);        gbc.gridx = 1;       gbc.gridy = 0;       add(textfield, gbc);    }     private class mypropertychangelistener implements propertychangelistener {       @override       public void propertychange(propertychangeevent evt) {          system.out.println("event received " + evt);           if (evt.getpropertyname().equals(propertychangemodel.changed)) {             textfield.settext(evt.getnewvalue().tostring());          }       }    }     private class buttonlistener implements actionlistener {       @override       public void actionperformed(actionevent e) {          controller.setcounter(controller.getcounter() + 1);          system.out.println("counter = " + controller.getcounter());       }    }  }  class propertychangecontroller {    private propertychangemodel model;     // public propertychangecontroller() {    // model = new propertychangemodel();    // }      public propertychangecontroller(propertychangemodel model) {       this.model = model;    }     public int getcounter() {       return model.getcounter();    }      public void setcounter(int i) {       model.setcounter(i);    } }  class propertychangemodel {    public static final string changed = "property change model updated";     private int counter;    private swingpropertychangesupport pcs;     public propertychangemodel() {       counter = 0;       pcs = new swingpropertychangesupport(this);    }     public void addpropertychangelistener(          propertychangelistener l) {       pcs.addpropertychangelistener(l);    }     public int getcounter() {       return counter;    }     public void setcounter(int i) {       int oldvalue = counter;       int newvalue = i;       counter = newvalue;       pcs.firepropertychange(changed, oldvalue, newvalue);       system.out.println("setcounter finished oldvalue=" + oldvalue             + ", newvalue=" + newvalue);    } } 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -