Category Archives: swing

Using SwingWorker to update Swing Components asynchronously

I have a JList, the contents of which are updated asynchronously by a different thread. The way I update the JList is by updating the model:

The only problem is, if this is not done in the EventDispatchThread, the UI becomes unresponsive, and starts behaving weird.

The way to solve this to use the SwingWorker.

I have a Jini server, which streams data to a Jini client, in this case, a Swing JFrame. I have a JList to which I publish the data as and when it becomes available.

Jini Service API

Its a simple streaming service:

At its heart is the interface RemoteDataListener, on to which the server publishes data as it becomes available.

Client Implementation

The key is to implement the interface RemoteDataListener along with the SwingWorker as shown below as an inner class in the JFrame:

We invoke the long running service in the doInBackground() method and add itself as a listener. When new data is received in the newData() method, we immediately call the publish() method, which delegates it to the process() method. The process() method is invoked within the eventDispatcherThread. This ensures that any updates made to the model of the JList is reflected on the UI. So, the code to update Swing components reside here. Also note that the done() method is called by the SwingWorker after the processing thread is finished.

Running the example

Run the SpringNonSecureRmiServer to start the Reggie and the Jini Server. After the Jini Server starts up, run the RmiStreamingDemoFrame.

rmi-data-streaming-1

rmi-data-streaming-2

Sources

The sources can be found here: https://github.com/paawak/blog/tree/master/code/jini/unsecure/streaming-with-jini

There are 3 Maven projects under that:

  1. rm-api
  2. rmi-client
  3. rmi-server

Using JXTreeTable to display grouped data

Let me start by ranting about Swing. Its a lame duck. It could never really take off. But we can’t live with it, can’t live without it. Especially if the application is a legacy Swing application written 10 years ago.

So, when I had the prospect of displaying simple grouped data, the fear of writing my custom JTree absolutely seized me. That is when i thought about the SwingX project. In its present form, it buggy, unusable, messed up etc. It took me close to an hour to figure out a stable release from the quagmire of broken links that Google came up with. At last, the Maven Central came to my rescue in the form of the following 3 magical lines:

The First 90%

After being associated with Swing for a good part of my life, I have come to believe that 90% of your job is done if you can create a good Model, especially for unwieldy components like JTable and JTree. So, I do the same for this as well. In this case, I represent grouped data with a Map<String, List<BankDetail>>. All I want is to display the groups as nodes and the Lists under these nodes. This is how I model it:

The remaining 10%

The tying up the model to the JXTreeTable is the simplest thing on the earth:

This is how it looks like:

UsingJXTreeTable_1UsingJXTreeTable_2

 

The source code can be found here:

https://github.com/paawak/blog/tree/master/code/lambda-demo

The main class to run is: GroupingDemoFrame.

I found the following post very useful in my efforts:

http://javanbswing.blogspot.com/2013/08/swing-treetable-example-using.html

Displaying large text in a JComboBox

Problem Statement

I have a list of texts, some of which are large, and I need to display them in a JComboBox. The trouble is, the JComboBox displays all the items in a single line. Consider that the following items need to be displayed:

  1. JNLP is supposed to be the latest and greatest in the Java Applet world post 1.6 release! Personally, I was never a great fan of Applets. I have always felt that Applets are a pain in the wrong place. Inherently difficult and cumbersome to use and deploy. But some of these concerns have been addressed
  2. This is really tiny
  3. As shown above, the 4 progress bars start one after another after the start button is clicked. The following code is for the JPanel with the 4 progress bars: @SuppressWarnings(“serial”) public class ProgressPanel extends JPanel {   private CountDownLatch startTrigger; private CountDownLatch endTrigger;   private JProgressBar bar1; private JSpinner spinner1;   private JProgressBar bar2; private

 

 

I am having the following code that displays a JComboBox in a JFrame:

It looks like this:

Large Text Combo Demo: Default ComboBox Renderer
Large Text Combo Demo: Default ComboBox Renderer

You can see it in action here:  

Note that the JComboBox stretches depending on the width of the largest text as rendered on a single line.

Limiting the width of the JComboBox

We can do that by setting the PrefrerredSize and the MaximumSize of the JComboBox:

It looks like this:

Large Text Combo Demo: Default ComboBox Renderer, With Max and Preferred Size Hints
Large Text Combo Demo: Default ComboBox Renderer, With Max and Preferred Size Hints

You can see it in action here:  

The problem with this approach is that the text is truncated.

Custom Renderer which wraps large test inside HTML

We will have a custom renderer for JComboBox which wraps this text into nice HTML so that it is displayed in multi-lines.

This is how the renderer looks like:

And this is how it looks like now:

Large Text Combo Demo: Custom ComboBox Renderer 1, With Max and Preferred Size Hints
Large Text Combo Demo: Custom ComboBox Renderer 1, With Max and Preferred Size Hints

You can see it in action here:  

As shown above, this does not look good when the pop-up is not there in the JComboBox.

Finishing touches

We need to make a distinction between when there is a pop-out and when there is not. This is done by the int index parameter in the ListCellRenderer::getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) method. When index == -1, it indicates that the item is not popped out. This is the code:

This is how it looks now:

Large Text Combo Demo: Custom ComboBox Renderer 2, With Max and Preferred Size Hints
Large Text Combo Demo: Custom ComboBox Renderer 2, With Max and Preferred Size Hints

You can see it in action here:  

The sources can be found https://github.com/paawak/blog/tree/master/code/Demo.