将项添加到另一个类的现有jlist中

我有一个使用NetBeans IDE中的Desing模式创建的jList(名为JList1),我想使用一个辅助类向该列表添加项目,该类解析一个大的xml列表并从中获取数据。 我的问题是,我真的不明白如何做到这一点,我已经尝试了很多不同的代码,尝试了一个模型,但不能正确。 我是java(也是编程)的新手,我不明白我是否做过类似的事情
String[] ar = {"one", "two", "three"};
JList Jlist1 = new JList(ar);

这创建了一个新的jList而不是使用我已创建的jList,不是吗?

 created using the Desing mode from NetBeans IDE, 
  • 也许不是一个好主意,因为它会产生一些代码

  • 将新Item添加到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); } }); } } 
 String[] ar = {"one", "two", "three"}; JList Jlist1 = new JList(ar); 

您正在使用的构造函数如下

 /** * Constructs a JList 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}. * 

* 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() { public int getSize() { return listData.length; } public E getElementAt(int i) { return listData[i]; } } ); }

因此,您需要将您作为参数传递给构造函数final的数组。 也可以使用generics。

 final String[] ar = {"one", "two", "three"}; JList Jlist1 = new JList(ar); 

最后,由于您使用的是新关键字,因此必然会创建新对象。 只需将原始列表指向使用数组创建的新JList对象即可。 请注意,您必须将其设为最终版,以后才能更改。