简体   繁体   English

更新Java Swing JList

[英]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在下面的GuiTest类中生成了大多数代码,除了在我添加(或更改)右边带有'/////////'的行之外。 WindowBuilder Pro (a WYSIWYG) keeps adding code to the constructor - but has not complained yet with me making alterations. WindowBuilder Pro(一种所见即所得)一直在向构造函数添加代码-但尚未对我进行更改表示抱怨。 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 . 我想以这种方式继续使用WindowBuilder Pro进行构建,因此我不想更改GUI的常规体系结构

I also created a DataTest class further below to emulate a certain slow process of gathering a data set. 我还在下面进一步创建了DataTest类,以模拟收集数据集的某些缓慢过程。

Here's my question: What I would like to do is update the JList with the new data from the DataTest class. 这是我的问题:我想用DataTest类中的新数据更新JList。

GuiTest Class: GuiTest类:

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: DataTest类:

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. 我还在下面进一步创建了DataTest类,以模拟收集数据集的某些缓慢过程。

Call it from a SwingWorker . SwingWorker调用它。


WindowBuilder Pro (a WYSIWYG).. WindowBuilder Pro(所见即所得)..

No, WYSIB (What You See Is Broken). 不,WYSIB(您看到的内容已损坏)。 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). Java布局管理器旨在根据PLAF,OS,屏幕分辨率,字体大小和版本之间的调整来计算组件的正确大小(最重要的是,可能还有更多)。 They also allow GUIs to be resizable. 它们还允许调整GUI的大小。 What WindowBuilder Pro has created with its use of setBounds() is a fragile GUI that is 'waiting to break'. WindowBuilder Pro使用setBounds()创建的是一个脆弱的GUI,正在“等待中断”。

For JList updates, you just need to work with the Model. 对于JList更新,您只需要使用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. 您正在将所有值添加到Data ...的构造函数调用中,最好编写任何自定义方法并将值传递给该方法,然后将值添加到JList模型中。 Model examples can be found Java JList model http://www.java2s.com/Tutorial/Java/0240__Swing/0750__JList-Model.htm 可以在Java JList模型中找到模型示例http://www.java2s.com/Tutorial/Java/0240__Swing/0750__JList-Model.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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