Creating a Font List Box in Java

This Java tutorial shows how to use a custom cell renderer to create a font list box. Below is a screen shot of the font list box that we will create in this tutorial:



In a previous tutorial, we showed how to get the names of the available fonts on your computer. This list of font names will be used to populate the items in the list box:

//Get the local graphics environment
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();

//Get the font names from the graphics environment
String[] fontNames = env.getAvailableFontFamilyNames();

//Create the JList with the font names
JList listBox = new JList(fontNames);
Next, the DefaultListCellRenderer will be extended to render the font names in that font. In the new class, we will override getListCellRendererComponent() method.
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
In this method, we will get an instance of the default cell renderer for this item:
//Get the default cell renderer
JLabel label = (JLabel) super.getListCellRendererComponent(list,
value,
index,
isSelected,
cellHasFocus);
Next, we will create a new font for the based on the value in the list box item:
//Create a font based on the item value
Font itemFont = new Font((String) value, Font.PLAIN, 24);
Since not all characters are displayable with every font, we will need to check to see if the font is capable of displaying the font name. If the font name is displayable with the font, we will use that font for displaying the the font name. However, if the font is not capable of displaying the font name, we will use the standard, but larger font.
if (itemFont.canDisplayUpTo((String) value) == -1)
{
//Set the font of the label
label.setFont(itemFont);
}
else
{
//Create a font based on the item value
String fontName = label.getFont().getFontName();
Font largerFont = new Font(fontName, Font.PLAIN, 24);

//Set the font of the label
label.setFont(largerFont);
}
The final step for this method is to return the modified renderer:
return label;
Below is the complete source for the FontCellRenderer class:
import javax.swing.*;
import java.awt.*;

public class FontCellRenderer extends DefaultListCellRenderer
{
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
//Get the default cell renderer
JLabel label = (JLabel) super.getListCellRendererComponent(list,
value,
index,
isSelected,
cellHasFocus);

//Create a font based on the item value
Font itemFont = new Font((String) value, Font.PLAIN, 24);

if (itemFont.canDisplayUpTo((String) value) == -1)
{
//Set the font of the label
label.setFont(itemFont);
}
else
{
//Create a font based on the item value
String fontName = label.getFont().getFontName();
Font largerFont = new Font(fontName, Font.PLAIN, 24);

//Set the font of the label
label.setFont(largerFont);
}

return label;
}
}
The final step is to set the renderer for the JList:

listBox.setCellRenderer(new FontCellRenderer());


2 comments:

Anonymous said...

How I do the same, but using a JComboBox???

Anonymous said...

thanx boss

Post a Comment