Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Jlist text center align

I have a JList on a panel.

How can I center align the text in the JList? I can't seem to find the settings anywhere for the model?

I have looked for align settings on the GUI but cant seem to find any there.

like image 916
R00059159 Avatar asked Dec 02 '22 20:12

R00059159


2 Answers

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);
like image 72
Hovercraft Full Of Eels Avatar answered Dec 13 '22 20:12

Hovercraft Full Of Eels


Try the following:

JList list = new JList(args);
DefaultListCellRenderer renderer =  (DefaultListCellRenderer)list.getCellRenderer();  
renderer.setHorizontalAlignment(JLabel.CENTER);  
like image 45
Mitesh Pathak Avatar answered Dec 13 '22 21:12

Mitesh Pathak