简体   繁体   中英

How to make a JList “refresh” method in a JPanel class

I'm a beginner at java and want to make a JFrame with tabs containing a seperate JPanel. One panel has a list where it displays things that you select in a different panel, so I want this panel to always display a list of stuff that you have selected in a different panel (I hope that makes sense). To do this, I need to make a method to refresh the JList. This is the Farthest that I've gotten on that:

public class PanelClass extends JPanel {
    private JList list;
    private DefaultListModel listModel = new DefaultListModel();
    private ArrayList<SomeOtherClass> objectArray = new ArrayList<SomeOtherClass>();

    public PanelClass() {
        list.setModel(listModel);
    }

    public void refresh() {
        updateListModel();
        list.setModel(listModel);
    }

    public void updateListModel() {
        if (objectArray.isEmpty()) {
            System.out.println("No Objects In Array!");
        } else {
            listModel.clear();
            for (SomeOtherClass SOC : objectArray) {
                // SOC.getName() just returns a string
                listModel.addElement(SOC.getName());
            }
        }
    }

    public void addObjectToArray(SomeOtherClass SOC) {
        objectArray.add(SOC);
    }
}

Could someone please tell me how to make a "refresh" method to constantly keep the JList up to date?

The AWT/Swing event model is based upon the widgets being event sources (in the MVC paradigm, they are both view and controller). Different widgets source different event types.

Look at the java.awt.event (primarily), and javax.swing.event packages for the listener interfaces you'll need to implement and register in order to produce your desired effect.

Basically, you write a Listener implementation, and register it with Widget1 . When Widget1 detects an event, it will notify you, and you can then use the information it provides to update Widget2 .

For instance, if a button being clicked would add an object to your list, you might have something like below (I usually put this code in the encompassing JFrame class, and make it implement the listener interfaces; but you can choose to use inner classes or separate listener classes):

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyFrame extends JFrame implements ActionListener {
  private JButton button = new JButton("Click me!");
  private DefaultListModel<String> listModel = new DefaultListModel<String>();
  private JList<String> list = new JList<String>(listModel);
  private int counter = 1;

  public MyFrame() {
    setTitle("Test Updates");

    JTabbedPane tabs = new JTabbedPane();
    add(tabs, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    panel.add(list);
    tabs.add("Selections", panel);

    panel = new JPanel();
    button.addActionListener(this);
    panel.add(button);
    tabs.add("Options", panel);

    pack();
  }

  @Override
  public void actionPerformed(final ActionEvent event) {
    if (button.equals(event.getSource())) {
      listModel.addElement("Item " + counter++);
    }
  }

  /* Test it! */
  public static void main(String[] args) {
    final MyFrame frame = new MyFrame();
    frame.addWindowListener(new WindowAdapter() {
      @Override public void windowClosing(final WindowEvent e) {
        frame.setVisible(false);
        frame.dispose();
        System.exit(0);
      }
    });

    frame.setVisible(true);
  }
}

This code sample is minimal, but it should give you an idea of how to go about implementing what you want.

You can do it in two way.
First : Write it in infinite thread loop so that it will constantly update JList.
Second : You can call refresh() method whenever new SOC objects are added in your ArrayList. It means you can call refresh() method from addObjectToArray() method which ultimately call the refresh method only when you have some change in your ArrayList.

FYI : I did it in my project and I went for second option.

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