If you are unfamiliar with SwingX, please read our SwingX introductory tutorial. SwingX introduces a lot of new components to Swing, and this tutorial will cover the task pane. You have seen task panes before if you’ve used Windows, they are used extensively in explorer and control panel. Here’s are examples:
What is a task pane?
The SwingX implementation of the task pane is more like Windows XP rather than Windows Vista, but the idea is the same. In a task pane, you can add various actions and components that are displayed in a list, which can be collapsed. You are free to add more than 1 task pane to a task pane container. You can also set an icon and title for your task pane. You can also set formatting options to change the colors used by the task pane and the task pane container.
You don’t have to use a task pane container to hold a task pane, you can add the task pane to any swing container. However, if you place the task pane in the task pane container, it will render better (SwingX changes the default colors of the task pane and container to match the OS it’s running on). You can override these default color schemes by adding key-value pairs in the UI Defaults yourself. This tutorial will cover all of this functionality.
Source code example
In the following example, I’m going to create a JXTaskPane, with some components, that I will then place in a JXTaskPaneContainer. Here is what it looks like:
The JXTaskPane called "My Tasks" has an icon and a title. It also contains a JXLabel with a background painter, and an Action object (which has a name, icon, and tooltip). When you click on the action, it simply changes the JXLabel’s text. Note that you can collapse the entire JXTaskPane by clicking anywhere on the title, or by pressing "Space" with your keyboard, when it’s in focus. You can change keyboard focus between the action and the JXTaskPane title by pressing the "Tab" key on your keyboard.
The first step in coding up this example is creating the JXTaskPane and JXTaskPaneContainers. The JXTaskPane also needs to be populated with an Action and JXLabel. Here’s the code to do this:
There are a couple of things to note here:
- There is an enumeration called Images, which is used to load some images that come with this example. I’ve added 3 PNG images to the icons.zip file that are included as part of the project download (it’s in the lib folder of the zip file). The enumeration has code in it which allows me to quickly load the images from PNG format, and then resize them to the width/height that is needed for this example. I use the GraphicsUtilities class that’s provided with SwingX, that has lots of useful features that you can use to resize images, etc. I also use the ImageIO class to actually read in the PNG file and convert it to a BufferedImage.
- There is a call to getPainter() which sets the background painter for the JXLabel. To learn more about Painters, and the Colors enumeration, click here. Here’s the code for getPainter():
- There is a call to changeUIDefaults() which will be explained below.
- You can add any component to the JXTaskPane. You can also add Actions. When using Actions, just make sure that the NAME key has a value. If you have a SHORT_DESCRIPTION value, then it will show up as a tooltip. If you have a SMALL_ICON or LARGE_ICON value, then it will show up as an icon in the JXTaskPane entry.
If you want to change the look of the JXTaskPane and JXTaskPane container, then you have to change some UI Defaults that they use to figure out how they should render themselves. There are quite a lot of options that you can set. Here’s the implementation of changeUIDefaults():
As you can see from this code, you can set 2 gradient colors for the JXTaskPane/JXTaskPaneContainer to paint the background. You have to set the "useGradient" value to "Boolean.TRUE". and you have to specify the "backgroundGradientStart" and "backgroundGradientEnd" values, which are both colors. Alternatively, you can set "useGradient" to false, and just use a "background" color. The code example shows you both combinations:
- The JXTaskPaneContainer doesn’t use a gradient fill, it uses a single background color (LightGray 50% transparent).
- The JXTaskPane uses a gradient fill (vertical fill), and it’s 2 stops are: White to LightBlue.
Here’s the complete listing for the TaskPanelExample1.java:
Please note that I’ve assembled the JXTaskPaneContainer in a JXPanel and I set it’s opacity to 70%, making it 30% transparent. This is another cool feature of using SwingX JXPanel, and it’s transparency. Learn more about it here.
To download the IDEA projects for this tutorial’s source code example, click here.
Comments and Feedback
To share your thoughts on this tutorial, please click here.