How to set selected index jcombobox by value?

SetSelectedItem("banana") You could have found it yourself by just reading the javadoc.

SetSelectedItem("banana"). You could have found it yourself by just reading the javadoc. Edit: since you changed the question, I'll change my answer.

If you want to select the item having the "banana" label, then you have two solutions: Iterate through the items to find the one (or the index of the one) which has the given label, and then call setSelectedItem(theFoundItem) (or setSelectedIndex(theFoundIndex)) Override equals and hashCode in ComboItem so that two ComboItem instances having the same name are equal, and simply use setSelectedItem(new ComboItem(anyNumber, "banana")).

Im sorry that I missed that obvious function, but I have updated my question as I forgot that I don't have "normal" items in my combobox – Arto Uusikangas Nov 30 at 14:47.

For example import java.awt. GridLayout; import java.awt.event. ActionEvent; import java.awt.event.

ActionListener; import javax.swing. JButton; import javax.swing. JComboBox; import javax.swing.

JFrame; import javax.swing. JOptionPane; import javax.swing. SwingUtilities; public class ComboboxExample { private JFrame frame = new JFrame("Test"); private JComboBox comboBox = new JComboBox(); public ComboboxExample() { createGui(); } private void createGui() { comboBox.

AddItem("One"); comboBox. AddItem("Two"); comboBox. AddItem("Three"); JButton button = new JButton("Show Selected"); button.

AddActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane. ShowMessageDialog(frame, "Selected item: " + comboBox.getSelectedItem()); javax.swing.SwingUtilities. InvokeLater(new Runnable() { @Override public void run() { comboBox.requestFocus(); comboBox.

RequestFocusInWindow(); } }); } }); JButton button1 = new JButton("Append Items"); button1. AddActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { appendCbItem(); } }); JButton button2 = new JButton("Reduce Items"); button2. AddActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { reduceCbItem(); } }); frame.

SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); frame. SetLayout(new GridLayout(4, 1)); frame.

Add(comboBox); frame. Add(button); frame. Add(button1); frame.

Add(button2); frame. SetLocation(200, 200); frame.pack(); frame. SetVisible(true); selectFirstItem(); } public void appendCbItem() { javax.swing.SwingUtilities.

InvokeLater(new Runnable() { @Override public void run() { comboBox. AddItem("Four"); comboBox. AddItem("Five"); comboBox.

AddItem("Six"); comboBox. SetSelectedItem("Six"); requestCbFocus(); } }); } public void reduceCbItem() { javax.swing.SwingUtilities. InvokeLater(new Runnable() { @Override public void run() { comboBox.

RemoveItem("Four"); comboBox. RemoveItem("Five"); comboBox. RemoveItem("Six"); selectFirstItem(); } }); } public void selectFirstItem() { javax.swing.SwingUtilities.

InvokeLater(new Runnable() { @Override public void run() { comboBox. SetSelectedIndex(0); requestCbFocus(); } }); } public void requestCbFocus() { javax.swing.SwingUtilities. InvokeLater(new Runnable() { @Override public void run() { comboBox.requestFocus(); comboBox.

RequestFocusInWindow(); } }); } public static void main(String args) { SwingUtilities. InvokeLater(new Runnable() { @Override public void run() { ComboboxExample comboboxExample = new ComboboxExample(); } }); } }.

Then you could access your combo box's items easily via their key Strings rather than ints. For instance... import java.util. HashMap; import java.util.

Map; import javax.swing. ComboBoxModel; import javax.swing.event. ListDataListener; public class MyComboModel implements ComboBoxModel { private Map nucleus = new HashMap(); // ... any constructors that you want would go here public void put(K key, V value) { nucleus.

Put(key, value); } public V get(K key) { return nucleus. Get(key); } @Override public void addListDataListener(ListDataListener arg0) { // TODO Auto-generated method stub } // ... plus all the other methods required by the interface }.

docs.oracle.com/javase/6/docs/api/javax/...) test. SetSelectedItem("banana"); There are some caveats or potentially unexpected behavior as explained in the javadoc. Make sure to read that.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions