Is there any way to right align the text in a JCombobox

Naeem Baghi picture Naeem Baghi · Oct 28, 2013 · Viewed 9.6k times · Source

I want to have a JComboBox with right align. how can I do that? someone before said "You can set a renderer to the JComboBox which can be a JLabel having JLabel#setHorizontalAlignment(JLabel.RIGHT)" but I don't know how can I do that?

Answer

camickr picture camickr · Oct 28, 2013

someone before said "You can set a renderer to the JComboBox which can be a JLabel having JLabel#setHorizontalAlignment(JLabel.RIGHT)"

Yes, the default renederer is a JLabel so you don't need to create a custom renderer. You can just use:

((JLabel)comboBox.getRenderer()).setHorizontalAlignment(JLabel.RIGHT);