a netbeans rcp blog

Auto collapsing JxTaskPane

Posted by Nicklas Löf on September 29, 2010

This post actually doesn’t have anything to do with Netbeans RCP but I thought I would share it anyway since it can be used in a Netbeans RCP application.

I added the JXTaskPane component from the SwingX package to my application. One thing that I didn’t like in my application was that multiple panes could be open at the same time. I would rather have the possibility to collapse the other ones automatically.

But looking in the javadoc didn’t really give any hints that this was a builtin feature into the JXTaskPane so I decided to implement it myself.

To do this is quite easy. First of all we need a collection of the Panes since there is no jxTaskPaneContainer.getPanes() method. It’s probably possible to use getComponents and implement a loop that checks if component instanceof JXTaskPane then….  But I preferred to keep my panes in a separate collection instead. In my application that works since the content is static and never changes but for dynamic content the other approach might be a better choice.

So..  lets add a collection of JXTaskPanes as a field. Also add the JXTaskPaneContainer as a field.

private final Collection<JXTaskPane> taskPanes = new ArrayList<JXTaskPane>();
private final JXTaskPaneContainer jXTaskPaneContainer = new JXTaskPaneContainer();

Then create a method that will take a created pane and add it to both the Collection and the PaneContainer.

private void addPane(JXTaskPane pane) {

Now we need a way to get informed that a pane has been uncollapsed. This can be done with the help of listeners. JXTaskPane doesn’t have any custom Listeners so we need to use a PropertyChangeListener. Easiest way is to implement one as a private class.

private static class CollapseListener implements PropertyChangeListener {
    private final MyTopComponent component;
    private final JXTaskPane pane;
    public CollapseListener(MyTopComponent component, JXTaskPane pane) {
        this.component = component;
        this.pane = pane;
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("collapsed") && evt.getNewValue().toString().equals("false")) {

Now we can see that there is one last method missing. The one that will take care of the actual auto collapsing.

private void collapseAllPanesExceptChoosen(JXTaskPane choosenPane) {
    for (JXTaskPane jXTaskPane : taskPanes) {
        if (!jXTaskPane.equals(choosenPane) &!jXTaskPane.isCollapsed()) {

And we almost done. We must add the listner to all the panes. Either we can do this in our addPane method or in another method that creates the panes or manually on each pane. It’s only one line of code

taskPane1.addPropertyChangeListener(new CollapseListener(this, taskPane1));
taskPane2.addPropertyChangeListener(new CollapseListener(this, taskPane2));

Done!  Now the previous taskpane that was open will be closed automatically when you click on another one.


One Response to “Auto collapsing JxTaskPane”

  1. Rinaldo said

    There is so many time you posted it, i don’t even know if you will gonna reply, anyway i need to try.
    First congrats for the nice tip you gave here. I am very new in Java and i am trying to learn, read asome books and the java tutorials in the oracle page. Now i am trying Netbeans and the tip you gave in this post is exactly what i was trying to do. But i tried to apply what you said here in a little Desktop program that i developing and an error keep coming again and again, maybe you can help. The error happens in this line

    private final MyTopComponent component;

    Cannot find symbol
    symbol: class MyTopComponent

    Do i need to create a class called MyTopComponent? Import some class? Please give some guidance here.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: