lunar
lunar

Reputation: 1232

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?

Upvotes: 1

Views: 8218

Answers (3)

Kallum Panditharatna
Kallum Panditharatna

Reputation: 101

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();
}

Upvotes: 0

Brian
Brian

Reputation: 7326

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);

}

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

A lot depends on information that you haven't told us, for instance just what is the JScrollPane holding? A JTable? A JList? The key will be to update the component being held by the JScrollPane and then revalidate and repaint this component.

Edit
You need to have a reference to the JList, so it should be declared outside of your constructor. For instance:

// GUI class
public class GuiClass {
   private JList items; // declare this in the *class*

   // class's constructor
   public GuiClass() {
     ArrayList<Item> i = g.getAllItems(); 
     Vector allItemsVector = new Vector(i); 

     // JList items = new JList(allItemsVector); // don't re-declare in constructor
     items = new JList(allItemsVector); 

     panel.add( new JScrollPane( items ))
   }

Then later in your menu's listener code you can add an item to the items JList as needed.

Upvotes: 2

Related Questions