JComboBox -- adding "Select Value" as Item

H

Hal Vaughan

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
 
B

Brandon McCombs

Hal said:
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

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.
 
H

Hal Vaughan

Brandon said:
Hal said:
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

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.

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
 
B

Brandon McCombs

Hal said:
Brandon said:
Hal said:
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
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.

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

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
 
H

Hal Vaughan

Brandon said:
Hal said:
Brandon said:
Hal Vaughan wrote:
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
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.

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

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

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
 
M

Michael Rauscher

Hal said:
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
 
R

Richard F.L.R.Snashall

Hal said:
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

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).
 
H

Hal Vaughan

Michael said:
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top