Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
How to set ID and Value for a JCombobox
Hasif Subair
Greenhorn
Posts: 24
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
public class DropDownItem { private String text; private int id; public void setText(String text) { this.text = text; } public void setId(int id) { this.id = id; } public String toString() { return text; } public int getId() { return id; }
I have get an array of objects of this class. I need to set the id and value on the JComboBox similar to that of a dropdownlist. So that I get the ID from the selected item.
Rob Camick
Rancher
Posts: 3324
32
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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 ); } }
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
JTable cell Editor/Renderer Question
Setting color in a JComboBox
about Hibernate
any idea about JComboBox Modal? with JDBC
Problem adding JComboBox to JFrame
More...