Skip to main content

Opinions: Efficient JComponent Sorting

4 replies [Last post]
Joined: 2004-03-04

Hi All. Happy 4th. I'm investigating a method for doing a low-overhead
means for inserting a JComponent into a given container. The insertion
is based on comparable using an insertion sort algorithm. The problem I'm
having is:

1) Each time a sort is done. A new array is created with the method
call Component[] children = JContainer.getComponents();
This is overhead I wish to avoid.

2) Because there is no JContainer.insert(aComponent,aComparator);
I resort to recreating the JContainer's component collection each time.

As an example:

Let say that I have
someContainer.add(new JButton("Pear"));

Then, later, as a result of some action. I then
do someContainer.add(new JButton("mango"));

What would be the most optimal method for sorting the buttons in someContainer.
Based on the text of the JButtons. Without having to recreate the collection
of components in someContainer. It would be nice if. I had a choice of
collection "TYPE" for JContainer.components. In other words.

TYPE could be SortedCollection ie.. Smalltalk
or a LinkedList

Thanks in advance.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2004-03-04

Thomas. I looked at your code. You're pretty heavy duty.
Thanks again. BTW. What project is this code from.

Joined: 2003-06-13

The code is from a framework for Toolbars that should prove to be much more mature compared to most stuff on the market today (really!)
That fits into the project as a whole to bring Swing to a professional level, and we combine it with a user interface compiler to enable your graphics designer build Swing applications using point and click.

More can be found at


Joined: 2003-06-13

Don't sort the array of the component; sort the widgets in the layout manager.

For an example see:

In method: [code] public void addLayoutComponent (Component component, Object constraint)[/code]

If you want it fast; make a layouter :) Does not have to be very complex... (the examples are quite complex, I know)

Joined: 2004-03-04

Thanks. This is perfect.