Java – how do I display an input dialog box with a drop-down list with icons for each item?
I use this java code in my swing application to display an input dialog box with a drop-down selection list so that users can select an item from the list:
String[] carModelsArray = { "Honda","Mitsubishi","Toyota" }; String selectedValue = (String)JOptionPane.showInputDialog( null,"Select a car model from the list below:","Car model...",JOptionPane.QUESTION_MESSAGE,null,carModelsArray,carModelsArray[ 0 ] );
This code works normally, but I want to know if I can also add an icon for each item in the selection list, so the drop-down selection list will be as follows:
I tried to set the items in this list as jlabel items, but when rendered in the drop-down list, the jlabel object is converted to a string value, just as it calls jlabel for each item in the list Get its value like the toString () method
Is there any way to achieve this goal?
Solution
Short answers, not your way The answer is long, more like
import java.awt.Component; import java.awt.EventQueue; import javax.swing.DefaultCombo@R_822_2419@Model; import javax.swing.DefaultListCellRenderer; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JCombo@R_822_2419@; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import org.kaizen.icons.AddIcon; import org.kaizen.icons.DeleteIcon01; import org.kaizen.icons.DeleteIcon02; public class Test { public static void main(String[] args) { new test(); } public test() { EventQueue.invokelater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | illegalaccessexception | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } DefaultCombo@R_822_2419@Model model = new DefaultCombo@R_822_2419@Model<Car>(); model.addElement(new Car(new AddIcon(16,16),"Honda")); model.addElement(new Car(new DeleteIcon01(16,"Mitsubishi")); model.addElement(new Car(new DeleteIcon02(16,"Tyota")); JCombo@R_822_2419@ cb = new JCombo@R_822_2419@(model); cb.setRenderer(new CarListCellRenderer()); int result = JOptionPane.showConfirmDialog(null,cb,"Select a car model from the list below",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE); if (result == JOptionPane.OK_OPTION) { Car car = (Car) cb.getSelectedItem(); } } }); } public static class CarListCellRenderer extends DefaultListCellRenderer { @Override public Component getListCellRendererComponent(JList<?> list,Object value,int index,boolean isSelected,boolean cellHasFocus) { System.out.println(value); super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus); if (value instanceof Car) { Car car = (Car) value; setIcon(car.getIcon()); setText(car.getText()); } else { setIcon(null); } return this; } } public static class Car { private Icon icon; private String text; public Car(Icon icon,String text) { this.icon = icon; this.text = text; } public Icon getIcon() { return icon; } public String getText() { return text; } } }
Basically, you need to control the combo box and provide your own custom listcellredender, which can provide the output you want