gpt4 book ai didi

java - 更新 Java Swing JList

转载 作者:行者123 更新时间:2023-11-30 07:27:14 25 4
gpt4 key购买 nike

我使用 WindowBuilder Pro 生成了下面 GuiTest 类中的大部分代码,带有“////////”的行除外我添加(或更改)的权利。 WindowBuilder Pro(所见即所得)一直在向构造函数中添加代码 - 但还没有提示我进行了更改。我想继续以这种方式使用 WindowBuilder Pro 进行构建,所以我不想更改 GUI 的总体架构

我还在下面进一步创建了一个 DataTest 类来模拟收集数据集的某个缓慢过程。

这是我的问题:我想要做的是用来自 DataTest 类的新数据更新 JList。

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

数据测试类:

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";
}
}

最佳答案

对于 JList 更新,您只需要使用模型即可。您在 Data... 的构造函数调用中添加所有值,最好编写任何自定义方法并将您的值传递给该方法,然后将值添加到 JList 模型中。可以找到模型示例Java JList model http://www.java2s.com/Tutorial/Java/0240__Swing/0750__JList-Model.htm

关于java - 更新 Java Swing JList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9866500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com