Re: how to sunchronise display of multiple JComboBoxes





On Oct 23, 7:27 pm, Rogan Dawes <disc...@xxxxxxxxxxxx> wrote:
sho...@xxxxxxxxx wrote:
Hi,
I want to show several JComboBoxes, on different locations of the
screen, where each of them has the same number of items but different
content (for example one list for username and one for real name). The
catch is that I want that when one of the JComboBoxes is opened (the
Popup is displayed) the others will be opened as well, and the
highlighted display change caused by the mouse will cause highlighting
of the matching items in the other JComboBoxes popups.

I managed to synchronize the selected items (after the popup is closed)
by extending ComboBoxModel and sharing same instance for all
JComboBoxes, and adding a custom cell renderer to each JComboBox with
index. This is not enough, and also I'm not sure this is the right way.

I can think of two approaches, but both seem like trouble:

1) add listeners to the JComboBoxes and hope I can synchronize all of
them without creating too much events (keeping flags to prevent several
selection events and so on).

2) inherit JComboBox, JComboBoxUI, BasicComboPopup, and maybe some
other classes, and use one instance to rule them all, which feels like
creating a new widget with all the overhead of that.

Is there some other simple way that I can't see?

Any hints?

Thanks,
Oren Shir

Well, you could add a PopupMenuListener to all of your JComboBox
instances. When it receives a "popupMenuWillBecomeVisible()" event,
iterate through all of your JComboBox'es, and do something like:

void popupMenuWillBecomeVisible(PopupEvent e) {
for (JComboBox c : comboBoxes) {
if (! c.isPopupVisible()) {
c.showPopup()
}
}

}void popupMenuWillBecomeInvisible(PopupEvent e) {
for (JComboBox c : comboBoxes) {
if (c.isPopupVisible()) {
c.hidePopup()
}
}

}That will at least show and hide the popups at the same time. I guess
you also want the selection to change at the same time, which this won't do.

Maybe you can change your approach, and use a single multi-column
JComboBox, to show all of the items?

I'm guessing that all of your comboboxes are lined up horizontally.
Maybe what you could do is use a custom renderer to show the information
from the separate combobox models in its own column?

Here is an example of how to use a multi-column renderer:

http://forum.java.sun.com/thread.jspa?threadID=692641

Good luck.

Rogan

Hi,

You were correct that my combo boxes are lined up horisontaly, but I'm
afraid that multi-column renderer is not good for me, because I neet it
to look like:
[Label("Username:")
][JComboBox(usernames)][Label("Name:")][JComboBox(realNames]

The problem I have with a single listener mechanism is that I need to
manage a flag to prevent multiple show/hide popup event being handled
by same listener. This can be done but I feel it is bad design - the
listener knows it is binding several comboBoxes, and the methods of the
listener are strongly coupled.

There are at least 2 ways to accomplish my task, but none seems like
good design. Could it be that the design of Combo Box class and
dependants is not flexible enough to offer complete binding of several
JComboBoxes?

Thanks,
Oren Shir

.