9

Currently I have this JComboBox, how can I get to center the content inside it?

String[] strs = new String[]{"15158133110", "15158133124", "15158133458"};
JComboBox com = new JComboBox(strs);

2 Answers 2

14

You need to create a Renderer then apply it to the JComboBox

DefaultListCellRenderer dlcr = new DefaultListCellRenderer(); 
dlcr.setHorizontalAlignment(DefaultListCellRenderer.CENTER); 
com.setRenderer(dlcr); 

also import this,

import javax.swing.DefaultListCellRenderer; 
2
  • 4
    I'm glad you got to that before I (foolishly) suggested HTML formatting. Now I can pretend I was never going to say that. ;) Commented Aug 23, 2012 at 3:57
  • 3
    I'd go further than 'much simpler'. Your answer is better classified as 'correct approach'. HTML formatting is a hack. Commented Aug 23, 2012 at 4:19
1
((JLabel)jComboBox1.getRenderer()).setHorizontalAlignment(SwingConstants.CENTER);

Try This

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.