如何更新JList?

时间:2014-01-20 06:59:35

标签: java swing jlist

我想从这个程序得到的是当按下第一个按钮时,它将显示分配的阵列,当按下第二个按钮是清除列表并显示分配的阵列时,我一直在寻找解决方案在线和许多人们建议使用列表模型,任何提示都表示赞赏。


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class tester extends JFrame{
private JList list = new JList();
private JButton b1 = new JButton("Array1");
private JButton b2 = new JButton("Array2");

public tester(){
    final String [] Array1 = {"1", "2", "3", "4"};
    final String [] Array2 = {"one", "two", "three", "four"};

    JPanel p1 = new JPanel();
    p1.add(new JScrollPane(list));
    p1.add(b1);
    p1.add(b2);
    add(p1, BorderLayout.CENTER);

    b1.addActionListener(new ActionListener (){
        public void actionPerformed(ActionEvent e){
            DefaultListModel Model1 = (DefaultListModel)list.getModel();
            for(int i = 0; i < Array1.length; i++){
                Model1.addElement(Array1[i]);
            }
        }
    });

    b2.addActionListener(new ActionListener (){
        public void actionPerformed(ActionEvent e){
            DefaultListModel Model2 = (DefaultListModel)list.getModel();
            for(int i = 0; i < Array2.length; i++){
                Model2.addElement(Array2[i]);
            }
        }
    });

}

public static void main (String [] args){
    tester t1= new tester();
    t1.pack();
    t1.setLocationRelativeTo(null);
    t1.setTitle("Test");
    t1.setVisible(true);
    t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}

3 个答案:

答案 0 :(得分:2)

您收到ClassDCastException,因为您试图将(DefaultListModel)投射到不是DefaultListModel的模型。您首先需要将模型设置为列表DefaultListModel,如此

    DefaultListModel model = new DefaultListModel();
    list.setModel(model);

当你这样做时,这将摆脱ClassCastException

    DefaultListModel Model1 = (DefaultListModel)list.getModel();

此外,您没有要清除的按钮,只能添加数组。因此,您需要清除按钮并在actionPerformed中使用

    model.removeAllElements();

这是您的代码的重构版本,可以使用

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class tester extends JFrame {

    private JList list = new JList();
    private JButton b1 = new JButton("Array1");
    private JButton b2 = new JButton("Array2");
    private JButton clear = new JButton("Clear");
    private DefaultListModel model;

    public tester() {
        final String[] Array1 = {"1", "2", "3", "4"};
        final String[] Array2 = {"one", "two", "three", "four"};

        model = new DefaultListModel();
        list.setModel(model);
        JPanel p1 = new JPanel();
        p1.add(new JScrollPane(list));
        p1.add(b1);
        p1.add(b2);
        p1.add(clear);
        add(p1, BorderLayout.CENTER);

        clear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                model.removeAllElements();
            }
        });
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DefaultListModel Model1 = (DefaultListModel) list.getModel();
                for (int i = 0; i < Array1.length; i++) {
                    Model1.addElement(Array1[i]);
                }
            }
        });

        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DefaultListModel Model2 = (DefaultListModel) list.getModel();
                for (int i = 0; i < Array2.length; i++) {
                    Model2.addElement(Array2[i]);
                }
            }
        });
    }

    public static void main(String[] args) {
        tester t1 = new tester();
        t1.pack();
        t1.setLocationRelativeTo(null);
        t1.setTitle("Test");
        t1.setVisible(true);
        t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

答案 1 :(得分:0)

试试这个:

b1.addActionListener(new ActionListener (){
    @Override
    public void actionPerformed(ActionEvent e){
        DefaultListModel Model1 = new DefaultListModel();
        for(int i = 0; i < Array1.length; i++){
            Model1.addElement(Array1[i]);
        }
        list.setModel(Model1);
    }
});

b2.addActionListener(new ActionListener (){
    @Override
    public void actionPerformed(ActionEvent e){
        DefaultListModel Model2 = new DefaultListModel();
        for (String Array21 : Array2) {
            Model2.addElement(Array21);
        }
        list.setModel(Model2);
    }
});

答案 2 :(得分:0)

  

这里有一个样本

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Main {
    private static int i = 1;

    public static void main(String args[]) {
        JFrame frame = new JFrame("Demo Frame/SuRu");
        Container contentPane = frame.getContentPane();
        Container contentPane2 = new JPanel(new GridLayout(3, 1));
        contentPane.setLayout(new BorderLayout());

        final DefaultListModel model = new DefaultListModel();
        final JList jList = new JList(model);
        JButton addButton = new JButton("Add Element");
        JButton removeButton = new JButton("Remove Element");
        JButton removeAllButton = new JButton("Remove All");

        contentPane2.add(addButton);
        contentPane2.add(removeButton);
        contentPane2.add(removeAllButton);
        contentPane.add(new JScrollPane(jList), BorderLayout.CENTER);
        contentPane.add(contentPane2, BorderLayout.EAST);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(100, 100, 500, 350);
        frame.setVisible(true);

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                model.addElement("Element " + i);
                i++;
            }
        });
        removeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (jList.getSelectedIndex() != -1) {
                    model.remove(jList.getSelectedIndex());
                }
            }
        });
        removeAllButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                model.removeAllElements();
            }
        });

    }
}