-1

I am making a GUI that adds a name to a list and then I have a JList on the home frame. When I hit the button on the other frame, I want it to update the JList to display the recently added string to the arraylist

ArrayList:

private static ArrayList<String> name = new ArrayList<String>();

JList:

JList lisName = new JList(name.toArray());

JButton I want to update the list:

JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        if(txtName.getText() == null) {
            JOptionPane.showMessageDialog(pnlName, "Error: Please enter name!");
        }else{
            name.add(txtName.getText());
            nameFrame.setVisible(false);
        }
        
    }
    
});
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Bravecity
  • 29
  • 2
  • 6
  • 1
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Sep 15 '17 at 00:29

2 Answers2

0

I would suggest you using a ListModel/TreeModel to handle JList, JTree etc.

There you can make something like this:

DefaultListModel listModel = new DefaultListModel(name);
JList jList = new JList(listModel); //or setListModel(listModel)

And when you want to update it, you can just use reload(), validate() or refresh(). So you can refresh the model of the list and everything's gonna be ok. Or you could just create new listModel and set the new values that you would like to show.

Since the question is similar to some others on Stackoverflow, you could check on this links and that should help you:

Also check related questions to yours. I hope I was helpful.

Strahinja
  • 440
  • 9
  • 26
  • 3
    @Bravecity `And when you want to update it, you can just use reload(), validate() or refresh().` - No, there is no need for any of those methods. All you need to do is update the ListModel and the model will tell the JList to repaint itself. Changes should always be made to a model, not an external data source (ie your ArrayList). – camickr Sep 14 '17 at 14:48
-1

I have been scouring dozens of answers on SO trying to solve this for the past two days. Others here have suggested to call yourJList.ensureIndexIsVisible(yourModel.getSize());, however this didn't work for me.

What worked for me was calling yourJList.requestFocus(); after addElement(String);. Hopefully, this helps some folks, as I was pretty frustrated over this one.

Edit: Changed naming convention.

the_mackster
  • 153
  • 2
  • 9
  • to give you the chance to improve the answer ;) which sounds fishy anyway - focus is unrelated to scrolling – kleopatra Aug 23 '22 at 04:17
  • No mention of scrolling in this post. Maybe get your facts straight before making condescending jabs of sarcasm. :) Was just trying to help people out. Have a nice life. Thanks for the input. – the_mackster Aug 23 '22 at 04:24
  • then why did you consider that ensureIndexIsVisible might be a solution to whatever problem you had? Seeing its javadoc: _Scrolls the list within an enclosing viewport to make the specified cell completely visible._ – kleopatra Aug 23 '22 at 07:16