How to let the content in JComboBox display in the center?

依然范特西╮ 提交于 2019-12-21 09:10:41

问题


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

回答1:


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:


((JLabel)jComboBox1.getRenderer()).setHorizontalAlignment(SwingConstants.CENTER);

Try This



来源:https://stackoverflow.com/questions/12084188/how-to-let-the-content-in-jcombobox-display-in-the-center

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