Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

Counting entries in a combo box.

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.
0
directxBOB
Asked:
directxBOB
  • 13
  • 7
  • 6
  • +3
5 Solutions
 
zzynxSoftware engineerCommented:
getItemCount()
0
 
CEHJCommented:
int n = ((DefaultComboBoxModel)combo.)getModel().getSize())
0
 
CEHJCommented:
Sorry

nt n = combo.getModel().getSize();
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
zzynxSoftware engineerCommented:
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 ;°)
0
 
zzynxSoftware engineerCommented:
combo.getItemCount() simply returns the number of items in the combo box
0
 
zzynxSoftware engineerCommented:
getComponentCount() is something different.
It returns the number of components in this container/panel
0
 
CEHJCommented:
>>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?
0
 
zzynxSoftware engineerCommented:
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.  // <<<<<<<<<<<<<
0
 
objectsCommented:
check is unnecessary, the selected index will be -1 or a valid index.
It won't be > size of combo

0
 
objectsCommented:
Just use getSelectedItem() to get the selected item.
It will return the item that is selected in your combo, or null.
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
     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.
0
 
zzynxSoftware engineerCommented:
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.
0
 
directxBOBAuthor 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.
0
 
objectsCommented:
assuming your model contains strings then just cast it

String selected = (String) combo.getSelectedItem();
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> Because getSelectedItem() returns the current object, which obviously has the text value.

String s = comboBox.getSelectedItem ().toString () ;
0
 
CEHJCommented:
>>assuming your model contains strings then just cast it

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

will work whether it does or not
0
 
zzynxSoftware engineerCommented:
>> 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
0
 
zzynxSoftware engineerCommented:
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.
0
 
zzynxSoftware engineerCommented:
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 + "";
    }
0
 
objectsCommented:
> 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.

0
 
zzynxSoftware engineerCommented:
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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 ().
0
 
CEHJCommented:
>>so it might be better to do a null-check before you use toString ().

Yes - was about to point this out myself
0
 
objectsCommented:
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().
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Yes, you can do a type-cast in that case. Check the object for null after that.
0
 
zzynxSoftware engineerCommented:
>> 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()
0
 
objectsCommented:
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.
0
 
objectsCommented:
not in real production swing code anyway.
0
 
zzynxSoftware engineerCommented:
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.
0
 
zzynxSoftware engineerCommented:
>> 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.
0
 
CEHJCommented:
:-)
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 13
  • 7
  • 6
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now