0

I am trying to update JList on multiple windows. How can I do that?

Here is my code, please help me.

samplListModel = new DefaultListModel<String>();
sampleList = new JList<String>(samplListModel);
samplListModel.addElement("Cities");
cityField = new JTextField();
cityField.setColumns(10);
cityButton = new JButton("Update");
JPanel panel = new JPanel();
panel.add(sampleList);
panel.add(cityField);
panel.add(cityButton);
this.add(panel);
this.setSize(500, 400);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
cityButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            samplListModel.addElement(cityField.getText());
            cityField.setText("");
        }
    });
lucian.pantelimon
  • 3,673
  • 4
  • 29
  • 46
Sathesh
  • 378
  • 1
  • 4
  • 13

2 Answers2

2

Just use the same DefaultListModel for both JList (meaning two) for each window. In example below I use a JDialog that you can type something, hit enter and you will see both lists updated. Not much to it.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class MultipleListDemo {

    private DefaultListModel model;
    private ListDialog dialog;

    public MultipleListDemo() {
        model = new DefaultListModel();
        JFrame frame = new JFrame();
        dialog = new ListDialog(frame, true);
        JList list = new JList(model);
        JScrollPane scroll = new JScrollPane(list);
        scroll.setPreferredSize(new Dimension(200, 300));

        JButton button = new JButton("Show Dialog");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(true);
            }
        });


        frame.add(scroll);
        frame.add(button, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public class ListDialog extends JDialog {

        public ListDialog(Frame frame, boolean modal) {
            super(frame, modal);

            JList list = new JList(model);
            JScrollPane scroll = new JScrollPane(list);
            scroll.setPreferredSize(new Dimension(200, 300));

            final JTextField field = new JTextField(15);
            field.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    model.addElement(field.getText());
                    field.setText("");
                }
            });

            setLayout(new BorderLayout());
            add(scroll);
            add(field, BorderLayout.NORTH);
            pack();
            setLocationRelativeTo(frame);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MultipleListDemo();
            }
        });
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

You have to use the same ListModel for both JLists to keep the lists in sync. For example you could create a class that extends DefaultListModel<String> and implement it as a singleton. For example:

class MyListModel extends DefaultListModel<String> {

    /**
     * The one and only instance of the class.
     */
    private static final MyListModel INSTANCE = new MyListModel();

    /**
     * We don't want any other instantiations. Hence make the constructor private.
     */
    private MyListModel() { }

    /**
     * Getter for the instance of this class.
     * 
     * @return the instance of this class
     */
    public static MyListModel getInstance() {
        return INSTANCE;
    }

}

Then you would create the JList with the singleton:

sampleList = new JList<String>(MyListModel.getInstance());

For all other operations on the ListModel you have to use MyListModel.getInstance(), too.

stevecross
  • 5,588
  • 7
  • 47
  • 85
  • Making your list model a singleton is not a good idea. It's better to have a GUI model where you pass the instance of that model to the various parts of the GUI. – Gilbert Le Blanc Mar 28 '14 at 10:18