Java Swing JXTable tutorial – fixing the ColumnControlButton menu

In this post I will show you how you can keep open the menu of ColumnControlButton in a Java Swing application using JXTable.

The reason for this JXTable tutorial

Want to know what problems you may encounter when using the JXTable component amd how to tackle them? Read on.

Through a weird and unwanted series of events (often referred to as a “daytime job”) a project landed on my plate which involves the development of a Java desktop application using Swing.

Being mainly involved so far in web project it’s the first time I work on a real Swing project. The project deals a lot with tables as it mimics the functionality of an excel sheet and it’s using SwingLabs’s API JXTable.

And now, the problem…

During the project I ran into an interesting request which regarding modifying the default behavior of the ColumnControlButton. The ColumnControlButton is a button in the header of tables that allows the user to show or hide columns of the table.

By default this button activates a small popup menu with check-boxes for each column and as you select one column the popup disappears. The request was that the popup stays such that the user can easily show or hide more columns in one go.

As I have spent a bit of time trying to figure this out on my own I thought it would be nice to share the solution with you.

The solution

The solution involves creating a custom ColumnControlButton and setting it on the table.

...
JXTable table;
//create the custom ColumnControlButton
table.setColumnControl(new MyCustomColumnControlButton());
//make it visible
table.setColumnControlVisible(true);
...

And now, for the custom ColumnControlButton class

public class MyColumnControlButton extends ColumnControlButton {
	/*
	 * This modifies the popup menu that hides/shows table
	 * columns such that the popup is kept open even after a column is clicked.
	 * The popup will close if the ColumnControlButton is pressed again.
	 */
	@Override
	protected ColumnControlPopup createColumnControlPopup() {
		return new DefaultColumnControlPopup() {
			@Override
			public void addVisibilityActionItems(List<? extends AbstractActionExt> actions) {

				ActionContainerFactory factory = new ActionContainerFactory(null);
				for (Action action : actions) {
					JMenuItem mi = factory.createMenuItem(action);
					mi.setUI(new MyCheckBoxMenuItemUI());
					addItem(mi);
				}
			}

		};
	}

	//we do a simple override or the code executed when a column selected for
	//showing or hiding
	public static class MyCheckBoxMenuItemUI extends BasicCheckBoxMenuItemUI {
		public static ComponentUI createUI(JComponent c) {
			return new MyCheckBoxMenuItemUI();
		}

		synchronized protected void doClick(MenuSelectionManager msm) {
			menuItem.doClick(0);
		}
	}
}

John Negoita

View posts by John Negoita
I'm a Java programmer, been into programming since 1999 and having tons of fun with it.
[jetpack-related-posts]

1 Comment

  1. Claudio HultgrenOctober 1, 2014

    Thanks man, I’ve been wondering how to do that for a while now, never gotten around to do it cause I had more pressing matters, but now that I stumbled on the solution, I’m going for it.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top