我有一个使用NetBeans IDE中的Desing模式创建的jList(名为JList1),我想使用一个辅助类向该列表添加项目,该类解析一个大的xml列表并从中获取数据。我的问题是,我真的不明白如何做到这一点,我已经尝试了很多不同的代码,尝试了一个模型,但不能正确。我是java(也是编程)的新手,我不明白我是否做了类似的事情
String[] ar = {"one", "two", "three"};
JList Jlist1 = new JList(ar);
这创建了一个新的jList,而不是使用我已经创建的,没有?
答案 0 :(得分:3)
created using the Desing mode from NetBeans IDE,
将新项添加到DefaultListModel
我希望使用辅助类将项添加到该列表中 解析一个大的xml列表并从中获取数据。
听起来像Concurency in Swing有问题,必须在EDT上对已经可见的Swing GUI进行更新
使用SwingWorker#publish()
进行长期和艰苦的工作(解析一个大的xml列表并从中获取数据。)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Testing extends JFrame {
private static final long serialVersionUID = 1L;
private DefaultListModel listModel = new DefaultListModel();
private JList list = new JList(listModel);
private int currentSelectedRow = 0;
private int xX = 0;
public Testing() {
setLocation(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
for (int x = 0; x < 9; x++) {
listModel.addElement("" + x);
xX++;
}
JScrollPane sp = new JScrollPane(list);
add(sp, BorderLayout.CENTER);
JButton btn1 = new JButton("Reset Model CastingModel");
add(btn1, BorderLayout.NORTH);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
list.clearSelection();
DefaultListModel model = (DefaultListModel) list.getModel();
model.removeAllElements();
// note Swing GUI by default to freeze if is removed more that
// 999 elemets from the JList or its underlaying XxxListModel,
// to use repeatly Actions from SwingTimer on short period
for (int x = 0; x < 9; x++) {
model.addElement("" + (x + xX));
xX++;
}
list.setModel(model);
}
});
JButton btn2 = new JButton("Reset Model directly from Model");
add(btn2, BorderLayout.SOUTH);
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
list.clearSelection();
listModel.removeAllElements();
for (int x = 0; x < 9; x++) {
listModel.addElement("" + (x + xX));
xX++;
}
}
});
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Testing().setVisible(true);
}
});
}
}
答案 1 :(得分:1)
String[] ar = {"one", "two", "three"};
JList Jlist1 = new JList(ar);
您使用的构造函数如下
/**
* Constructs a <code>JList</code> that displays the elements in
* the specified array. This constructor creates a read-only model
* for the given array, and then delegates to the constructor that
* takes a {@code ListModel}.
* <p>
* Attempts to pass a {@code null} value to this method results in
* undefined behavior and, most likely, exceptions. The created model
* references the given array directly. Attempts to modify the array
* after constructing the list results in undefined behavior.
*
* @param listData the array of Objects to be loaded into the data model,
* {@code non-null}
*/
public JList(final E[] listData)
{
this (
new AbstractListModel<E>() {
public int getSize() { return listData.length; }
public E getElementAt(int i) { return listData[i]; }
}
);
}
因此,您需要将您传递的数组作为构造函数final的参数传递。也要使用泛型。
final String[] ar = {"one", "two", "three"};
JList<String> Jlist1 = new JList<String>(ar);
最后,由于您使用的是新关键字,因此必须创建新对象。只需使您的原始列表指向使用您的数组创建的新JList对象。请注意,你必须让它成为最终的,以后不能改变。