我有一个使用 NetBeans IDE 中的 Desing 模式创建的 jList(名为 JList1),我想使用辅助类将项目添加到该列表中,该辅助类解析一个大的 xml 列表并从中获取数据。我的问题是我真的不明白如何做到这一点,我已经尝试了很多不同的代码,也尝试了一个模型,但无法做到正确。我是java新手(也是编程新手),我不明白我是否做这样的事情
创建了一个新的jList而不是使用我已经创建的一个,不是吗?
String[] ar = {"one", "two", "three"};
JList Jlist1 = new JList(ar);
问问题
3474 次
2 回答
3
created using the Desing mode from NetBeans IDE,
被生成的代码囚禁可能不是一个好主意
将新项目添加到 DefaultListModel
我想使用辅助类将项目添加到该列表中,该辅助类解析一个大的 xml 列表并从中获取数据。
听起来您在 Swing 中遇到 Concurency问题,必须在 EDT 上完成对已经可见的 Swing GUI 的更新
用于
SwingWorker#publish()
长期而艰巨的工作(解析一个大的 xml 列表并从中获取数据。)
例如,向 DefaultListModel 添加一个新 Item
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);
}
});
}
}
于 2013-08-16T10:19:10.953 回答
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);
最后,由于您使用的是 new 关键字,因此它必然会创建新对象。只需使您的原始列表指向使用您的数组创建的这个新的 JList 对象。请注意,您必须使其成为最终版本,并且以后无法更改。
于 2013-08-16T10:14:10.030 回答