简体   繁体   中英

Update a Java Swing JList

I used WindowBuilder Pro to generate most of the code in the GuiTest Class below with the exception of lines with '////////' to the right which I added (or altered). WindowBuilder Pro (a WYSIWYG) keeps adding code to the constructor - but has not complained yet with me making alterations. I want to keep building with WindowBuilder Pro in this manner, so I don't want to make changes to the general architecture of the GUI .

I also created a DataTest class further below to emulate a certain slow process of gathering a data set.

Here's my question: What I would like to do is update the JList with the new data from the DataTest class.

GuiTest Class:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener; ///////////
import javax.swing.JList;
import javax.swing.JLabel;

import discoverTool.DataTest;

public class GuiTest extends JFrame implements ListSelectionListener{
    private String[] foo =  {"thing1","thing2","thing3"}; //////////////

    private JPanel contentPane;
    private JLabel lblNewLabel; //////////////made global

    /**
     * Launch the application.
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws InterruptedException {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GuiTest frame = new GuiTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        DataTest dt = new DataTest();/////////////
        GuiTest gt = new GuiTest();
        gt.foo = dt.foo;
        gt.updateJList(dt.foo);
    }

    public void updateJList(String[] f){
        //reset the list with f
    }

    /**
     * Create the frame.
     */
    public GuiTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JList list = new JList(foo);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); /////////////
        list.setSelectedIndex(0);               //////////////
        list.addListSelectionListener(this);    //////////////      
        list.setBounds(10, 11, 134, 240);
        contentPane.add(list);

        JPanel panel = new JPanel();
        panel.setBounds(154, 11, 188, 81);
        contentPane.add(panel);
        panel.setLayout(null);

        //JLabel lblNewLabel = new JLabel("New label");/////////
        lblNewLabel = new JLabel("New label");///////////
        lblNewLabel.setBounds(10, 11, 46, 14);
        panel.add(lblNewLabel);
    }

    public void valueChanged(ListSelectionEvent e) {///////////////
        JList jList = (JList)e.getSource();////////////
        lblNewLabel.setText( foo[jList.getSelectedIndex()] );///////////
    }///////////
}

DataTest class:

public class DataTest {

    public String[] foo;

    public DataTest() throws InterruptedException{
        //simulate a long process getting data;
        Thread.sleep(7000);
        foo = new String[4];
        foo[0]="hey";
        foo[1]="hi";
        foo[2]="bye";
        foo[3]="adios";
    }
}

Either add the new data to the model, or create a new model with all the data and set that as the new model for the list.


I also created a DataTest class further below to emulate a certain slow process of gathering a data set.

Call it from a SwingWorker .


WindowBuilder Pro (a WYSIWYG)..

No, WYSIB (What You See Is Broken). Java layout managers are designed to calculate the correct size of components based on PLAF, OS, screen resolution, font size, and tweaks between versions (off the top of my head, there are probably more). They also allow GUIs to be resizable. What WindowBuilder Pro has created with its use of setBounds() is a fragile GUI that is 'waiting to break'.

For JList updates, you just need to work with the Model. You are adding all the values inside constructor call in Data... , better you write any custom method and pass your values into that method and then add the values in the JList model. Model examples can be found Java JList model http://www.java2s.com/Tutorial/Java/0240__Swing/0750__JList-Model.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM