Java Jlist text center align

a 夏天 提交于 2019-12-01 21:22:48
Hovercraft Full Of Eels

This has nothing to do with the model since it involves the view, the ListCellRenderer to be specific. One solution; get the renderer and set its horizontalAlignment to SwingConstants.CENTER. Assuming that you're not using a custom cell renderer you could for example do:

DefaultListCellRenderer renderer = (DefaultListCellRenderer) myJList.getCellRenderer();
renderer.setHorizontalAlignment(SwingConstants.CENTER);

Try the following:

JList list = new JList(args);
DefaultListCellRenderer renderer =  (DefaultListCellRenderer)list.getCellRenderer();  
renderer.setHorizontalAlignment(JLabel.CENTER);  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!