简体   繁体   English

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

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

I have a jList (named JList1) created using the Desing mode from NetBeans IDE, and I want to add items to that list using a secondary class which parses a big xml list and gets the data from it. 我有一个使用NetBeans IDE中的Desing模式创建的jList(名为JList1),我想使用一个辅助类向该列表添加项目,该类解析一个大的xml列表并从中获取数据。 My problem is that I dont really understand how to do this, I already tried a lot of different codes, tried with a model too, but cant get it right. 我的问题是,我真的不明白如何做到这一点,我已经尝试了很多不同的代码,尝试了一个模型,但不能正确。 I am new to java (and programming too), and I dont understand if I do something like 我是java的新手(也是编程),如果我做的话,我也不明白
String[] ar = {"one", "two", "three"};
JList Jlist1 = new JList(ar);

this created a new jList instead of using my already created one, no ? 这创建了一个新的jList,而不是使用我已经创建的,没有?

created using the Desing mode from NetBeans IDE,
  • maybe not good idea to be prisonier of code generated by 也许并不是一个好主意,因为它会产生一些代码

  • add a new Item to DefaultListModel 将新项添加到DefaultListModel

and I want to add items to that list using a secondary class which parses a big xml list and gets the data from it. 我想使用一个辅助类将项添加到该列表中,该辅助类解析一个大的xml列表并从中获取数据。

  • sounds like as you have an issue with Concurency in Swing , updates to the already visible Swing GUI must be done on EDT 听起来像Swing中的Concurency有问题,必须在EDT上对已经可见的Swing GUI进行更新

  • use SwingWorker#publish() for long and hard job (which parses a big xml list and gets the data from it.) 使用SwingWorker#publish()进行长期和艰苦的工作(解析一个大的xml列表并从中获取数据。)

for example, add a new Item to DefaultListModel 例如,将新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);

The constructor you are using is as follows 您正在使用的构造函数如下

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

So you need to have your array which you are passing as an argument to the constructor final. 因此,您需要将您传递的数组作为构造函数final的参数传递。 Also do make use of generics. 也可以使用泛型。

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

Lastly since you are using new keyword it is bound to create new object. 最后,由于您使用的是新关键字,因此必须创建新对象。 Just make your original list point to this new JList object created using your array. 只需使您的原始列表指向使用您的数组创建的新JList对象。 Mind you have to make it final and cannot be changed later. 请注意,你必须让它成为最终的,以后不能改变。

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

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