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. <br /><strong>First</strong> : Write it in infinite thread loop so that it will constantly update JList. <br /><strong>Second</strong> : 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.


  • Merkle tree for difference comparison, in Cassandra
  • Can I recreate a temp table after dropping it?
  • Tabbing behavior in WebForms
  • What is the format of in git-branch --list
  • Adding multiple files with JFileChooser
  • How to best utilize Kubernetes/minikube DNS for local development [closed]
  • How to make a marker appear or disappear based on zoom level on Google Maps v2
  • Display message in a toastr after controller method finish
  • Latency Time in Load Testing
  • How to set imageview over imageview?
  • Powershell, Excel Finding string in cell, Color row, delete others
  • SQL Developer 4.0.2 install on Windows 7 64 bit
  • offset parameter of curand_init
  • How to generate a SHA256 hash of 32 bytes using nodejs (crypto) in order to avoid bad key size error
  • Linking to an id not working in Firefox - Working in Chrome/Safari
  • pyodbc fails without error
  • How can I save Array of Class Objects in to a Plist(Iphone Development)
  • Hyperledger fabric's ChannelCreationPolicy
  • Android: Mediaplayer stop / start playing raw resource
  • JavaFX Embed Custom Font Not Working
  • E: Malformed entry 1 in list file /etc/apt/sources.list.d/sbt.list (Suite)
  • why 'read' command in shell script is missing initial characters? [duplicate]
  • Implementing type equation generator in OCaml
  • How to escape xpath in php
  • Laravel 5 - Cache remember doesn't work
  • Complex multiple if statements
  • Unable to connect to Azure MySQL Database through Azure Function - C#
  • Issues with converting data into a matrix after running lapply()
  • internal javascript not works in angular2
  • Extracting a small subset of data from XMLs
  • Set WebClient.Builder.exchangeStrategies() without losing Spring Jackson configuration
  • Regex not working in java 1.5
  • C++ STL stack pop operation giving segmentation fault
  • Conflicting declaration using constexpr and auto in C++11
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • ssh remote server login script
  • Passing value from popup window to parent form's TextBox
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • WPF custom control and direct content support
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication