How to center elements in Java combobox

Is there a method for combobox in Java that will center elements in combobox? I tried this, but this did not work:

myCombobox.setAlignmentY(CENTER_ALIGNMENT);

Thank!

+5
source share
2 answers

Try this link: How to use combo boxes (Java ™ Tutorials> Creating a GUI Using 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);
+10
source

Have you considered the concept Renderersdescribed in JTable textbook pages oracles , teak concept - is similair to JComboBox, JList, JTableand JTree, in Rendereryou can center the desired text

+5
source

All Articles