We help IT Professionals succeed at work.

Counting entries in a combo box.

directxBOB
directxBOB asked
on
Medium Priority
220 Views
Last Modified: 2010-03-31
I want to get the cound of the number of values in a combo box.

if((combo.getSelectedIndex() > -1)&&(combo.getSelectedIndex() <  combo.getComponentCount())){
                  
}

Basically I want to try and protect against Array out of Bounds exceptions, because when an array out of bounds occurs I want to use:

combo.getSelectedItem();

So the idea is to use the index value, if its array out of bounds, then we know the value isnt in the list, so we check the user has edited the combo and use that value.
Comment
Watch Question

zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
getItemCount()
CERTIFIED EXPERT
Top Expert 2016

Commented:
int n = ((DefaultComboBoxModel)combo.)getModel().getSize())
CERTIFIED EXPERT
Top Expert 2016
Commented:
Sorry

nt n = combo.getModel().getSize();

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
So my comment in your previous Q
>> It looks like the code triggered after the selection doesn't expect other values than the predefined ones.
was rather to the point after all ;°)
zzynxSr. Software engineer
CERTIFIED EXPERT
Commented:
combo.getItemCount() simply returns the number of items in the combo box
zzynxSr. Software engineer
CERTIFIED EXPERT
Commented:
getComponentCount() is something different.
It returns the number of components in this container/panel
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>then we know the value isnt in the list

Not quite sure what you mean by that, but taking a guess, wouldn't

boolean presentInList = list.contains(combo.getSelectedItem());

do it?
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
So your code snippet becomes:

if ( (combo.getSelectedIndex() > -1) && (combo.getSelectedIndex() <  combo.getItemCount()-1) ) {
               
}

But I think to
>> check the user has edited the combo

getSelectedIndex() is all you need.

It returns the first item in the list that matches the given item.
The result is not always defined if the JComboBox allows selected items that are not in the list.
Returns -1 if there is no selected item or if the user specified an item which is not in the list.  // <<<<<<<<<<<<<
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
check is unnecessary, the selected index will be -1 or a valid index.
It won't be > size of combo

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Just use getSelectedItem() to get the selected item.
It will return the item that is selected in your combo, or null.
Siva Prasanna KumarPrincipal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
     String[] nums = { "1", "2", "3", "4", "5" };
      JComboBox numbers = new JComboBox(nums);
                System.out.printlnn(numbers.getItemCount());

this program will print the number of items present in combo box.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
directxBob,
It's already from your previous post that I don't completely understand your problem:
If your combo is editable getSelectedItem() just returns an item that is part of the predefined items or the item entered by the user in the combo.

>> Just use getSelectedItem() to get the selected item
>> It will return the item that is selected in your combo, or null.
Null? In what case?

shivaspk,
No need to repeat that information.
I think I was rather clear.

Author

Commented:
Is there anyway to get the string used in a combo box out of the combo box without using the Object, the reason I ask is because Im using IDL's and handling strings is far easier.

Because getSelectedItem() returns the current object, which obviously has the text value.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
assuming your model contains strings then just cast it

String selected = (String) combo.getSelectedItem();
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> Because getSelectedItem() returns the current object, which obviously has the text value.

String s = comboBox.getSelectedItem ().toString () ;
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>assuming your model contains strings then just cast it

String selected = combo.getSelectedItem().toString();

will work whether it does or not
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> Is there anyway to get the string used in a combo box out of the combo box without using the Object
Just apply toString() to the object
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
btw, the standard way whatever objects are rendered in a combo box
is by just calling the toString() method on the objects.
So you can influence the way your objects are represented in your combo box by overriding
the toString() method in your object class.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Example:

public class MyObject {
   private String name;
   private int ID;

   public MyObject(int id, String name) {
       this.name = name;
       this.ID = id;
   }

    public String toString() {
       return name;
       // return ID + "";
    }
}

If you want your objects in the combo box to be displayed by their name use the toString() as above.
If you want them to be displayed by their ID, just adapt your toString() method:

    public String toString() {
       return ID + "";
    }
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> So you can influence the way your objects are represented in your combo box by overriding
> the toString() method in your object class.

A custom renderer is actually the recomended way of achieving it. toString() is not really intended for that purpose.

zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
I don't deny that. It's to illustrate
>> the standard way whatever objects are rendered in a combo box
>> is by just calling the toString() method on the objects.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Though I suggested toString () myself, I agree that in other cases, my own choice is a type-cast as it prevents a method-call (and in the process, creation of activation records on the OS stack, pushing, popping, etc) and also prevents a NullPointerException if the reference is referring to null. Since in the case of a JComboxBox, getSelectedItem () might not always return a String, it would be better to use toString (). But maybe that it could return null if nothing is selected, so it might be better to do a null-check before you use toString ().
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>so it might be better to do a null-check before you use toString ().

Yes - was about to point this out myself
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
But you know what type your model contains so you can cast it to that type and if it isn't a String then you can call whatever method is required to get the string you need. Typically this will *not* be toString().
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Yes, you can do a type-cast in that case. Check the object for null after that.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> Typically this will *not* be toString().

Since the question was:
>> Is there anyway to get the string used in a combo box
it *will* typically be toString()
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
No typically the combo will contain Strings, or it will use a renderer that accesses the methods of the object.
As I already stated toString() should not be used.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
not in real production swing code anyway.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Typically the combo will contain whatever objects and typically the default renderer will be used.
And that default renderer calls the object's toString() method.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> not in real production swing code anyway.
I think you would be surprised how much combo's in production swing code use the default renderer.
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.