How to center items in a Java combobox

北战南征 提交于 2020-08-27 06:15:06

问题


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!


回答1:


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);



回答2:


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



来源:https://stackoverflow.com/questions/11008431/how-to-center-items-in-a-java-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!