簡體   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