Search code examples
javaswingconcurrencyjlistswingworker

How can I add String JList element from another JPanel/JFrame/etc?


I know how to actually add the element to the JList itself, but I am unsure how to make my Event Log (which uses the JList along with a DefaultListModel) update as the updates actually occur. It seems as though my application freezes up then all entries are shown when it's over. Please help?


Solution

  • I'm 100% sure that you've got a Swing concurrency issue where you're tying up the Swing event dispatch thread or EDT with a long-running process. The problem is that the Swing even thread is responsible for performing all Swing drawing and user interactions. If it is tied up with some long running process such as file or database input or output, then it can't perform its responsibilities, and the whole GUI becomes frozen. The solution is to use a background thread such as a SwingWorker, so that the long-running process doesn't tie up the EDT, but taking care to update Swing components such as the JList or its model on the EDT.

    Please check out the Swing concurrency tutorial for more details on the problem and its solution.