JComboBox setting label and value

前端 未结 5 1537
死守一世寂寞
死守一世寂寞 2020-11-29 11:01

Is it possible to set a value and a label to a JComboBox so I can show a label but get a value that is different?

For example in JavaScript I can do:

5条回答
  •  醉酒成梦
    2020-11-29 11:45

    Use ListCellRenderer to achieve what you want. Make a class that extends JLabel and implements ListCellRenderer. Set that class as a renderer in your JComboBox using setRenderer() method. Now when you access values from your jcombobox it will be of type jlabel.

提交回复
热议问题