| Author |
Change the font in a JList
|
Vani Shastri
Ranch Hand
Joined: Aug 17, 2006
Posts: 52
|
|
Hi, I am unable to change the font of the items in the JList. Is there a way to do it? Thanks.
|
 |
Ankur Sharma
Ranch Hand
Joined: Dec 27, 2005
Posts: 1234
|
|
Originally posted by Vani Shastri: Hi, I am unable to change the font of the items in the JList. Is there a way to do it? Thanks.
Dear Vani, Are you interested to change the font for each item present in JList or you simple want to change the font for all items in JList....
|
The Best way to predict your future is to create it
Ankur Sharma
|
 |
Anand Loni
Ranch Hand
Joined: Jan 20, 2006
Posts: 150
|
|
Hi Vani, If you want to set same font for each item then you can simply set font for the list using, list.setFont(new Font("Arial",Font.BOLD,14)); I have used Font Name as Arial here you can use others. If you want to change font of particular item from list you have change this in renderer of that list. As renderer for list uses JLabel as each item you can explicitly set font for that label. Regards, Anand
|
~ Anand,
SCJP 1.5
SCWCD 1.5
|
 |
Vani Shastri
Ranch Hand
Joined: Aug 17, 2006
Posts: 52
|
|
Thanks a lot Ankur and Anand. But there is another doubt. I want to change the font of just one particular item in the JList. Is it possible?
|
 |
Anand Loni
Ranch Hand
Joined: Jan 20, 2006
Posts: 150
|
|
Hi, As I have told already in previous reply, you can change font of particular item of the list in renderer. Sample code would be class MyListCellRenderer extends DefaultListCellRenderer { JLabel label; Font font; MyListCellRenderer() { font = new Font("Areal", Font.BOLD,14); } public Component getListCellRendererComponent(JList list, // The list Object value, // value to display int index, // cell index boolean isSelected, // is the cell selected boolean cellHasFocus) // the list and the cell have the focus { label = (JLabel)super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus); if(//condition to select particular label whose font to be set) { label.setFont(font) ; } else { label.setFont(list.getFont()); } } } Hope this helps..
|
 |
Vani Shastri
Ranch Hand
Joined: Aug 17, 2006
Posts: 52
|
|
Thanks Anand Have a nice day
|
 |
 |
|
|
subject: Change the font in a JList
|
|
|