简体   繁体   中英

How to add Items to JList from another window

I have 2 windows. one got an empty JList and the other one got a button. So I want to add the value to the list whenever I press the button. Here is my code but not completed:

Window 1

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

Window 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.

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. 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:

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:

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);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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