JComboBox Values

Is it possible to define values ​​other than the actual contents in a JComboBox?
In HTML, it looks like this:

<select> <option value="value1">Content1</option> <option value="value2">Content2</option> <option value="value3">Content3</option> </select> 

Here you can get a short value, no matter how long its contents.

In Java, I only know the following solution:

 // Creating new JComboBox with predefined values String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; private JComboBox combo = new JComboBox(petStrings); // Retrieving selected value System.out.println(combo.getSelectedItem()); 

But here I would only get "Cat", "Dog", etc.

The problem is that I want to load all client names from the database into JComboBox, and then get the identifier from the selected client. It should look like this:

 <select> <option value="104">Peter Smith</option> <option value="121">Jake Moore</option> <option value="143">Adam Leonard</option> </select> 
+4
source share
3 answers

If you create the Customer class and load the list of Customer objects into the combobox, then you get what you want. The toString () of your object will be displayed in the combo box, so the Customer class must return the name in toString (). When you retrieve the selected item, it is a Customer object from which you can get the identifier or something else that you want.


Here is an example illustrating what I am proposing. However, it would be nice to follow the advice of kleopatra and mKorbel when you get this basic idea.

 import java.awt.BorderLayout; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class ComboJumbo extends JFrame{ JLabel label; JComboBox combo; public static void main(String args[]){ new ComboJumbo(); } public ComboJumbo(){ super("Combo Jumbo"); label = new JLabel("Select a Customer"); add(label, BorderLayout.NORTH); Customer customers[] = new Customer[6]; customers[0] = new Customer("Frank", 1); customers[1] = new Customer("Sue", 6); customers[2] = new Customer("Joe", 2); customers[3] = new Customer("Fenton", 3); customers[4] = new Customer("Bess", 4); customers[5] = new Customer("Nancy", 5); combo = new JComboBox(customers); combo.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e) { Customer c = (Customer)e.getItem(); label.setText("You selected customer id: " + c.getId()); } }); JPanel panel = new JPanel(); panel.add(combo); add(panel,BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 200); setVisible(true); } class Customer{ private String name; private int id; public Customer(String name, int id){ this.name = name; this.id = id; } public String toString(){ return getName(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } } } 
+6
source

Assuming you have a Person class that contains names and identifiers, you can add instances of this class as combo box objects. getSelectedItem() will then return an instance of the selected face.

The problem is to display the person correctly in the combo box. You can overload toString in the class to return the name, or you can provide your own ListCellRenderer in the combo box and display whatever you want (for example, thumbnails of photos) in the combo box.

+3
source

I just answered another question at fooobar.com/questions/471451 / ... , which explains a good way to create a custom ListCellRenderer that replaces toString() with a value class with alternative labels.

0
source

Source: https://habr.com/ru/post/1400768/


All Articles