Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center items in a Java combobox

Is there a method for a combobox in Java that will center the items in the combobox? I tried this but it didn't work:

myCombobox.setAlignmentY(CENTER_ALIGNMENT);

Thanks!

like image 518
AndroidDev Avatar asked Jun 13 '12 05:06

AndroidDev


2 Answers

Try this link: How to Use Combo Boxes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

class ComboBoxRenderer extends JLabel
                   implements ListCellRenderer {
  public ComboBoxRenderer() {
    setOpaque(true);
    setHorizontalAlignment(CENTER);
    setVerticalAlignment(CENTER);
  }
  //. . .

or

((JLabel)comboBox.getRenderer()).setHorizontalAlignment(SwingConstants.CENTER);
like image 186
aterai Avatar answered Sep 19 '22 13:09

aterai


You have look at Renderers concept, described about in the JTable tutorial on Oracles pages, thic concept is similair for JComboBox, JList, JTable and JTree, in the Renderer you can centering desired text

like image 23
mKorbel Avatar answered Sep 22 '22 13:09

mKorbel