繁体   English   中英

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

[英]Adding items to an already existing jlist from another class

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

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

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

  • 将新项添加到DefaultListModel

我想使用一个辅助类将项添加到该列表中,该辅助类解析一个大的xml列表并从中获取数据。

  • 听起来像Swing中的Concurency有问题,必须在EDT上对已经可见的Swing GUI进行更新

  • 使用SwingWorker#publish()进行长期和艰苦的工作(解析一个大的xml列表并从中获取数据。)

例如,将新Item添加到DefaultListModel

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 <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对象。 请注意,你必须让它成为最终的,以后不能改变。

暂无
暂无

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

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