Java: Swing JComboBox, is it possible to have hidden data for each item in the list?

Import java.awt. *; import java.awt.event. *; import java.util.

*; import javax.swing. *; import javax.swing.plaf.basic. *; public class ComboBoxItem extends JFrame implements ActionListener { public ComboBoxItem() { Vector model = new Vector(); model.

AddElement( new Item(1, "car" ) ); model. AddElement( new Item(2, "plane" ) ); model. AddElement( new Item(3, "train" ) ); model.

AddElement( new Item(4, "boat" ) ); model. AddElement( new Item(5, "boat aadf asfsdf a asd asd" ) ); JComboBox comboBox; // Easiest approach is to just override toString() method // of the Item class comboBox = new JComboBox( model ); comboBox. AddActionListener( this ); comboBox.

PutClientProperty("JComboBox. IsTableCellEditor", Boolean. TRUE); getContentPane().

Add(comboBox, BorderLayout. NORTH ); // Most flexible approach is to create a custom render // to diplay the Item data comboBox = new JComboBox( model ); comboBox. SetRenderer( new ItemRenderer() ); comboBox.

AddActionListener( this ); getContentPane(). Add(comboBox, BorderLayout. SOUTH ); } public void actionPerformed(ActionEvent e) { JComboBox comboBox = (JComboBox)e.getSource(); Item item = (Item)comboBox.getSelectedItem(); System.out.

Println( item.getId() + " : " + item.getDescription() ); } class ItemRenderer extends BasicComboBoxRenderer { public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { super. GetListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if (value! = null) { Item item = (Item)value; setText( item.getDescription().toUpperCase() ); } if (index == -1) { Item item = (Item)value; setText( "" + item.getId() ); } return this; } } class Item { private int id; private String description; public Item(int id, String description) { this.Id = id; this.

Description = description; } public int getId() { return id; } public String getDescription() { return description; } public String toString() { return description; } } public static void main(String args) { JFrame frame = new ComboBoxItem(); frame. SetDefaultCloseOperation( EXIT_ON_CLOSE ); frame.pack(); frame. SetVisible( true ); } }.

– evilReiko Mar 3 at 10:05 Vector is not deprecated (at least in JDK6). Swing components use Models to store the data. The DefaultComboBoxModel uses a Vector to store the data.

If you don't like this you can always create your own Model and use whatever you want to store the data. – camickr Mar 3 at 16:10.

Create a User class which has the attributes username and id; return only username in .toString().

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


Thank You!
send