'How to refresh JScrollPane

My main frame contains JScrollPane which lists some objects. Through menu (pop up frame) I create new object and I want to list this object in the JScrollPane (which is created in a constructor of DemoFrame class). How can I do it?

Part of my constructor in DemoFrame

    ArrayList<Item> i = g.getAllItems(); 
    Vector allItemsVector = new Vector(i); 
    JList items = new JList(allItemsVector); 
    panel.add( new JScrollPane( items ))

In pop up frame I add new object to 'g' object in that case. Have I designed it wrong?



Solution 1:[1]

This was a problem for me as well. A quick workaround is remove the JScrollPane from the panel, make your changes then readd it. Many may deem it inefficient, but it works with no significant change to runtime

JPanel panel = new Jpanel();
JList list = new JList({"this", "is", "a test", "list"});
JScrollPane sPane = new JScrollPane();

public void actionPerformed(ActionEvent e) {
  if (resultsPane!=null){
panel.remove(sPane);
  }

  sPane = updatePane(list);         
  panel.add(sPane);
}

public void updatePane(String[] newListItems) {
  DefaultListModel model = new DefaultListModel();  
  for(int i = 0; i < newListItems.length; i++) {
    model.addElement(newListItems[i]);
  }

JList aList = new JList(model);
JScrollPane aPane = new JScrollPane(aList);

}

Solution 2:[2]

Assuming I understood the question correctly: You have a JFrame with a JScrollPane. And the JScrollPane has a JList. The JList has a set of strings in it and this shows fine, but later you want to update the list and have it update in the JFrame view, but when you do update the list nothing happens to visualisation?

I had the same problem and how I got it to work was to set the content of the JList using a DefaultListModel. Instead of updating the contents of the JList, I update the contents in the DefaultListModel and then set the JList content to the model content when everything has been added. Then call the JScrollPane repaint function

//Declare list and pane variables up here so that we have a reference
JList<String> list;
JScrollPane pane;

//Set up the frame content
void SetupFrame() {

    //instantiate the list
    list = new JList();

    //sets up the scroll pane to take the list
    pane = new JScrollPane();
    pane.setViewportView(list);
    pane.setBounds(10, 10, 200, 80);

    //adds the scrollpane to the frame
    add(pane);

    //update the list of strings
    UpdateList();
}

//Updates the contents of the list (call this whenever you want to update the list)
void UpdateList() {

    //model used to update the list
    DefaultListModel model = new DefaultListModel();

    //Update the contents of the model
    for (int i = 0; i < 10; i++)
        model.addElement("Test value");

    //update the list using the contents of the model
    connectionLabels.setModel(model);

    //repaint the scrollpane to show the new list content
    pane.repaint();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brian
Solution 2 Kallum Panditharatna