JComboBox help again but different help 75+ items in it
so my program works like this you select an option from 1 combo box that determines what goes in a second combo box..this works nicely but each selection adds from 25-35 items each in to the second combo box for a total of around 100+ items
each option from the second combo box sets a different number to an integer and each choice has a different number so my question is
is there a better way to set the integer for each of these than to write 100+ if statements? i looked at the jcombobox api and didnt see anything that could help me...or maybe the only way is to write a bunch of if statements? any ideas on how i could do this without having a 2mb file of if statements? lol
Re: JComboBox help again but different help 75+ items in it
You could put the value you want returned when a item is selected in the item itself.
See the ClientProperty methods for JComponent. Its frontends a hash map where you can store any value you want in each component.
Re: JComboBox help again but different help 75+ items in it
I can't tell whether Norm is referring to what I've done below or not. Was your comment one suggestion or two, Norm?
I remember from a recent thread that JComboBox takes an array of Objects in one of its constructors, so the visible items must be the result of .toString() on the original objects. You can create a convenience class to offer your JComboBox items' displayable content via the .toString() method, and bundle whatever other values you like as member data in each object:
Code java:
package com.javaprogrammingforums.domyhomework;
import java.awt.event.*;
import java.math.BigDecimal;
import javax.swing.*;
public class JComboObject extends JFrame
{
public static final long serialVersionUID = 42;
public JComboObject()
{
ComboItem[] options = new ComboItem[]
{
new ComboItem("One", new BigDecimal(1))
, new ComboItem("Two", new BigDecimal(2))
, new ComboItem("Three", new BigDecimal(3))
};
setSize(200, 200);
JComboBox jcb = new JComboBox(options);
add(jcb);
jcb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(JComboObject.this, ((ComboItem)((JComboBox)event.getSource()).getSelectedItem()).getValue().toString());
}
});
}
public static void main(String[] args)
{ new JComboObject().setVisible(true); }
}
class ComboItem
{
private final String key;
private final BigDecimal value;
public ComboItem(String k, BigDecimal v)
{ key = k; value = v; }
/* toString() is what is displayed in the combobox dropdown */
public String toString()
{ return key.toString(); }
public BigDecimal getValue()
{
return value;
}
}
Apologies for the actionPerformed - it saved some space but looks horrible. There are two casts and an outer-class-this in there.
Re: JComboBox help again but different help 75+ items in it
Your solution has no overlap with mine. See the JComponent class's ClientProperty methods.
For example:
final String TheKey = "A KEY";
JComboBox jcb1 = new JComboBox(...);
jcb1.setClientProperty(TheKey, theValue);
...
then get the reference to the source of the event into a JComponent object and
TheValueType theValue = (TheValueType)src.getClientProperty(TheKey);
Re: JComboBox help again but different help 75+ items in it
Ah ok ... it was the first bit which threw me. I thought after I'd tested out the JComboBox(Object[]) idea, that it was exactly what you were suggesting by
Quote:
in the item itself.
Re: JComboBox help again but different help 75+ items in it
Your class provides get/set methods for storing values for each item in the list.
Re: JComboBox help again but different help 75+ items in it
i see how that could work for me but im having trouble understanding how to use it...i read the clientproperty methods in the jcomponent api but i cant get my head around it
what is the key? the value i think i understand but the key is throwing me off
Re: JComboBox help again but different help 75+ items in it
The client property uses something like a hashmap. Look at the API doc for that class.
The key is what you give to a hashmap for it to return the value.