Diego
Diego

Reputation: 49

Swing. Fill JScrollPane with JList

I'm doing a window aplication. I need to create a JScrollPane and then fill it with a String[]. The problem is that I need to refresh it.

I want an empty JScrollPane when I start the aplication and when I press any button, the button will refresh the JScrollPane.

I have all the code, I only need refresh the content:

    // Empty JScrollPane
    scrollPaneDic = new JScrollPane(new JList());
    scrollPaneDic.setBounds(225, 75, 200, 170);

    // Layout
    frame.getContentPane().setLayout(null);
    frame.getContentPane().add(lblPalabrasDiccionario);
    frame.getContentPane().add(scrollPaneDic);

And in the button actionListener is where I have to refresh the content:

    String[] newContent = (method generate and return the new array);
    listDic = new JList(newContent);
    scrollPaneDic = new JScrollPane(listDic);

Important: scrollPaneDic and listDic are global

Thanks

Upvotes: 0

Views: 365

Answers (1)

VGR
VGR

Reputation: 44308

Changing a variable's value does not affect a component's state in any way; you must call a component method. Until you do that, your frame has no way of knowing that you created a new JScrollPane.

Actually, you should not create a new JScrollPane or a new JList. Instead, you should update the existing JList:

JList listDic = (JList) scrollPaneDic.getViewport().getView();
listDic.setListData(newContent);

Also, using a null layout is strongly discouraged, especially for a JScrollPane. Try resizing your window, or try changing the size of your system fonts before you run the application, and you will see why. Not every user's system is like yours.

Upvotes: 2

Related Questions