Results 1 to 9 of 9
  1. #1
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    44
    Rep Power
    0

    Default How do I update a JList when a property of UserObject changes ?

    In the SSCCE below I have a JList and I change the value (label) of the selected Item (MyObject class) pressing the jButton.
    How do I update my JList to show the new value ?
    Thanks

    ------------------ edit ------------------
    I want to avoid repaint() method because it has some overhead. This method is used for LAF.
    As @gimbal2 said I am looking for the correct solution.
    -----------------------------------------
    Java Code:
    public class UpdateJList extends JFrame implements ActionListener {
    
        JList list;
        DefaultListModel<MyObject> listModel;
    
        public UpdateJList() {
    
            listModel = new DefaultListModel();
            listModel.addElement(new MyObject("Item 1"));
            listModel.addElement(new MyObject("Item 2"));
            listModel.addElement(new MyObject("Item 3"));
            listModel.addElement(new MyObject("Item 4"));
    
            list = new JList();
            list.setModel(listModel);
    
            JButton btn = new JButton("Change Label");
            btn.addActionListener(this);
    
            add(list);
            add(btn);
            setLayout(new FlowLayout());
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(150, 150);
        }
    
        public static void main(String args[]) {
            new UpdateJList().setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e) {
            MyObject myObject = (MyObject) list.getSelectedValue();
    
            if (myObject != null) {
                myObject.setLabel("New");
               // list.repaint();
            }
        }
    
        class MyObject {
    
            String label;
    
            public MyObject(String label) {this.label = label;}
    
            public void setLabel(String label) {this.label = label;}
    
            public String toString() {return this.label;}
        }
    }
    Last edited by kosmos890; 02-13-2015 at 01:12 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How do I update a JList when a property of UserObject changes ?

    Your question should be different: is calling repaint() the correct way to get it to update? Because uncommenting that line of code actually makes it work.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How do I update a JList when a property of UserObject changes ?

    The problem as I see it is you are updating object state in a MyObject instance and the listModel doesn't know it. There are a variety of ways to do this but in the end, a repaint will be done, otherwise you won't see the change. Check out ListDataListeners and PropertySupport in the Java tutorials. See the link in my signature. And BTW, JList is a generic type so you may want to fix the declarations and assignments. Finally, it is best to extend JPanel, not JFrame. Then put the panel instance in a JFrame.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    44
    Rep Power
    0

    Default Re: How do I update a JList when a property of UserObject changes ?

    @gimbal2
    thanks for your reply.
    I am looking for a method like nodeChanged of DefaultTreeModel.

    ListDataListener doesn't help me because it fires an event when the contents of the JList change.

    I use PropertyChangeSupport but I cant update the selected item.
    Java Code:
    public class UpdateJList extends JFrame implements ActionListener, PropertyChangeListener {
    
        JList list;
        DefaultListModel<MyObject> listModel;
    
        public UpdateJList() {
    
            listModel = new DefaultListModel();
            
            listModel.addElement(new MyObject("Item 1"));
            listModel.addElement(new MyObject("Item 2"));
            listModel.addElement(new MyObject("Item 3"));
            listModel.addElement(new MyObject("Item 4"));
            
            for(int i = 0 ; i < listModel.getSize() ; i++){
                listModel.getElementAt(i).addPropertyChangeListener(this);
            }
            
            list = new JList();
            list.setModel(listModel);
    
            JButton btn = new JButton("Change Label");
            btn.addActionListener(this);
    
            add(list);
            add(btn);
            setLayout(new FlowLayout());
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(150, 150);
        }
    
        public static void main(String args[]) {
            new UpdateJList().setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e) {
            MyObject myObject = (MyObject) list.getSelectedValue();
    
            if (myObject != null) {
                myObject.setLabel("NEW");
            }
        }
        
        public void propertyChange(PropertyChangeEvent evt) {
            System.out.println("PropertyChangeEvent New Value :  "+evt.getNewValue());
            //How to update the selected object ?
        }
      
        class MyObject {
    
            String label;
            PropertyChangeSupport pcs;
    
            public MyObject(String label) {
                this.label = label;
                pcs = new PropertyChangeSupport(this);
            }
    
            public void setLabel(String label) {
                String oldValue = this.label;
                pcs.firePropertyChange("label", oldValue, label);
                this.label = label;
            }
    
            public String toString() {
                return this.label;
            }
            
            public void addPropertyChangeListener(PropertyChangeListener listener){
                pcs.addPropertyChangeListener(listener);
            }
        }
    }

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How do I update a JList when a property of UserObject changes ?

    Quote Originally Posted by kosmos890 View Post
    II want to avoid repaint() method because it has some overhead.
    Bollocks. Create as complex a UI as you like in a JPanel and run this (maybe form a button's action litener):
    Java Code:
    System.out.println(System.currentTimeMillis());
    panel.repaint();
    System.out.println(System.currentTimeMillis());
    Quote Originally Posted by kosmos890 View Post
    This method is used for LAF.
    Huh?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    44
    Rep Power
    0

    Default Re: How do I update a JList when a property of UserObject changes ?

    @DarrylBurke
    I confuse updateUI and repaint. Sorry.

    I suppose that Ι can use repaint method.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How do I update a JList when a property of UserObject changes ?

    I am still uncertain why you are reluctant to explicitly call repaint. But you could also just reset the value after you change the label.

    For example:

    listModel.set(index, myObject);

    I tried it so I know it will work. Of course, internally this fires an event which results in a repaint. The documentation does not discuss trying to set an object which compares equally to the one already in place. However, I would implement the equals and hashCode methods in MyObject class anyway. I believe the PropertySupport I recommended is just extra overhead and not really needed.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    44
    Rep Power
    0

    Default Re: How do I update a JList when a property of UserObject changes ?

    @jim829
    Thanks for your reply
    Quote Originally Posted by jim829 View Post
    I am still uncertain why you are reluctant to explicitly call repaint.
    My application has two JLists and their contents changes very often. I am not sure if it is indicated to call repaint frequently.

    I try this as you suggested and it works. Finally I prefer the repaint because it is simpler.
    Java Code:
        public void actionPerformed(ActionEvent e) {
    //        MyObject myObject = (MyObject) list.getSelectedValue();
    //        if (myObject != null) {
    //            myObject.setLabel("NEW");
    //            repaint();
    //        }
    
            int selectedIndex = list.getSelectedIndex();
            
            if(selectedIndex > -1){
                MyObject mo = listModel.get(selectedIndex);
                mo.setLabel("NEW");
                listModel.set(selectedIndex, mo);
            }
        }

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How do I update a JList when a property of UserObject changes ?

    Quote Originally Posted by kosmos890 View Post
    I am not sure if it is indicated to call repaint frequently.
    You don't seem to realize that nothing changes on screen without a repaint.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How to update 4 jlist in one jpanel
    By nikolas in forum AWT / Swing
    Replies: 14
    Last Post: 01-13-2015, 02:02 PM
  2. Trying to update a jList
    By kmccmk9 in forum AWT / Swing
    Replies: 3
    Last Post: 11-07-2013, 06:26 AM
  3. Update JList problem
    By ostkanten in forum New To Java
    Replies: 5
    Last Post: 09-25-2012, 04:53 AM
  4. Update Jlist in JFrame
    By dragon_baby in forum AWT / Swing
    Replies: 4
    Last Post: 05-26-2012, 02:07 PM
  5. Cannot get my JList to update!
    By rangvald in forum AWT / Swing
    Replies: 1
    Last Post: 11-17-2009, 01:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •