简体   繁体   English

如何从另一个窗口向 JList 添加项目

[英]How to add Items to JList from another window

I have 2 windows.我有2个窗户。 one got an empty JList and the other one got a button.一个有一个空的 JList,另一个有一个按钮。 So I want to add the value to the list whenever I press the button.因此,每当我按下按钮时,我都想将该值添加到列表中。 Here is my code but not completed:这是我的代码但未完成:

Window 1窗口 1

final DefaultListModel<String> favouriteNames = new DefaultListModel<String>();
JList namesList = new JList(favouriteNames);

Window 2窗口 2

public class button extends JFrame {

private JList namesList;
private DefaultListModel<String> favouriteNames;

this.namesList = namesList;

 JButton addThis = new JButton("Add");
 addThis.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
          favouriteNames.addElement("Jack");
   }
 });
}
}

Pass an instance of your DefaultListModel to Window 2 in the constructor.在构造函数中将 DefaultListModel 的一个实例传递给 Window 2。

Edited to add: Here's how you pass an instance in a constructor.编辑添加:这是在构造函数中传递实例的方法。

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.SwingUtilities;

public class ButtonFrame implements Runnable {

    private JFrame              frame;

    private DefaultListModel    favouriteNames;

    public ButtonFrame(final DefaultListModel favouriteNames) {
        this.favouriteNames = favouriteNames;
    }

    @Override
    public void run() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton addThis = new JButton("Add");
        addThis.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                favouriteNames.addElement("Jack");
            }
        });

        frame.add(addThis);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ButtonFrame(new DefaultListModel()));
    }

}

I made a simpler version of my program, but still have a problem, I believe the ActionPerformed sends the data but the JList does not recognise it or or basically did not expect to receive it.我制作了一个更简单的程序版本,但仍然有问题,我相信 ActionPerformed 发送了数据,但 JList 无法识别或基本上没有期望收到它。 So here is what I have done so far.所以这是我到目前为止所做的。 So it is a bit more of my research and attempts, maybe it gives more details about the problem.所以这是我的更多研究和尝试,也许它提供了有关问题的更多详细信息。

Main Window:主窗口:

public class main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame = new ClassA();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

}

ClassA: A类:

public class ClassA extends JFrame {

        DefaultListModel<String> myList;
        JList list;

        public ClassA() {

        setSize(300,200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2,1));

        myList = new DefaultListModel<String>();
        list = new JList(myList);


        //ClassB sendsText = new ClassB(myList, list);

        JButton find = new JButton("Find");
        find.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                new ClassB().setVisible(true);
            }

        });


        add(panel);
        panel.add(find);
        panel.add(list);
    }

}

ClassB: B类:

public class ClassB extends JFrame {

    DefaultListModel<String> myList;
    JList list;
    public ClassB(DefaultListModel<String> myList, JList list){
        this.myList = myList;
        this.list = list;
    }


    public ClassB() {
        setSize(300,200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2,1));

        JButton addMe = new JButton("Add Me");
        addMe.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                myList.addElement("Danial");

            }

        });

        add(panel);
        panel.add(addMe);
    }

}

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

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