When you set the selectedIndex on a JComboBox to -1, it makes a blank choice
visible in the box since no item is selected. Is there any way to add a
message like, "Select Item Style" so it shows up if no item is chosen?
I'm using Java 1.4.2 if that makes a difference.
Thanks!
Hal
Brandon McCombs - 16 Sep 2006 04:21 GMT
> When you set the selectedIndex on a JComboBox to -1, it makes a blank choice
> visible in the box since no item is selected. Is there any way to add a
[quoted text clipped - 5 lines]
>
> Hal
I presume you have a String[] already setup that contains all the
entries that appear in your JComboBox? If so, just add "Select Item
Style" as the first string in the array. Then when no item is chosen
(such as when the GUI is first drawn and the user hasn't set a value for
the combobox) programmatically call combobox.setSelectedIndex(0),
instead of setSelectedIndex(-1) and you will have what you want.
Hal Vaughan - 16 Sep 2006 05:23 GMT
>> When you set the selectedIndex on a JComboBox to -1, it makes a blank
>> choice
[quoted text clipped - 13 lines]
> the combobox) programmatically call combobox.setSelectedIndex(0),
> instead of setSelectedIndex(-1) and you will have what you want.
I was thinking of that. But once something was selected, I didn't want that
item to be able to be selected. Is there a way to specify that an item is
not selectable? Such as similar components in other GUIs that have lines
separating different groups of items? In other words, a way to put an item
in the list that cannot be selected because it is more for information than
as a choice?
Hal
Brandon McCombs - 16 Sep 2006 06:10 GMT
>>> When you set the selectedIndex on a JComboBox to -1, it makes a blank
>>> choice
[quoted text clipped - 21 lines]
>
> Hal
I don't know of a way to not let it be selected using the default JDK
methods but you could probably rig something up inside the method that
is called for the combobox's actionPerformed() and detect when that
entry is selected and proceed accordingly.
On the other hand when a valid entry is selected you can use the builtin
methods removeItemAt() to dynamically remove the "Select Item Style"
entry and at the appropriate times use insertItemAt() to insert the
entry again.
hope that helps
Hal Vaughan - 16 Sep 2006 06:49 GMT
>>>> When you set the selectedIndex on a JComboBox to -1, it makes a blank
>>>> choice
[quoted text clipped - 36 lines]
>
> hope that helps
That's about what I figured.
I was just hoping there was a built in way to do it that I didn't know
about.
Thanks!
Hal
Michael Rauscher - 16 Sep 2006 10:13 GMT
Hal Vaughan schrieb:
> When you set the selectedIndex on a JComboBox to -1, it makes a blank choice
> visible in the box since no item is selected. Is there any way to add a
> message like, "Select Item Style" so it shows up if no item is chosen?
>
> I'm using Java 1.4.2 if that makes a difference.
Brandon already gave you a solution but there's another way which I'd
call the Swing way ;)
Since your question is related to the presentation of some situation
only and since Swing follows some MVC concept, I'd prefer to handle the
situation in the view.
You can use e. g. the following renderer (or you can design it as a
decorator to have a more generic solution):
class ComboBoxCellRenderer extends DefaultListCellRenderer {
private String selectionPrompt;
private JComboBox comboBox;
public ComboBoxCellRenderer( JComboBox comboBox,
String selectionPrompt ) {
this.comboBox = comboBox;
this.selectionPrompt = selectionPrompt;
}
public Component getListCellRendererComponent( JList list,
Object value, int ix, boolean sel, boolean focus ) {
super.getListCellRendererComponent( list, value, ix,
sel, focus );
if ( ix == -1 && comboBox.getSelectedIndex() == -1 )
setText( selectionPrompt );
return this;
}
};
Bye
Michael
Hal Vaughan - 16 Sep 2006 21:06 GMT
> Hal Vaughan schrieb:
>> When you set the selectedIndex on a JComboBox to -1, it makes a blank
[quoted text clipped - 36 lines]
> Bye
> Michael
I already have a wrapper class for all my Swing components because they fit
in with a set of data tables I'm using, so I can add that to the wrapper
for a JComboBox fairly easily. That's just about the kind of thing I was
looking for.
Thanks!
Hal
Richard F.L.R.Snashall - 16 Sep 2006 10:25 GMT
> When you set the selectedIndex on a JComboBox to -1, it makes a blank choice
> visible in the box since no item is selected. Is there any way to add a
[quoted text clipped - 5 lines]
>
> Hal
I wonder if you could set up two Combo Box models which differ by an
entry and then, after the initial selection, surreptitiously replace
the model (with the appropriate alteration of the selected value).