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 environmentNext, the DefaultListCellRenderer will be extended to render the font names in that font. In the new class, we will override getListCellRendererComponent() method.
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);
public Component getListCellRendererComponent(JList list,In this method, we will get an instance of the default cell renderer for this item:
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
//Get the default cell rendererNext, we will create a new font for the based on the value in the list box item:
JLabel label = (JLabel) super.getListCellRendererComponent(list,
value,
index,
isSelected,
cellHasFocus);
//Create a font based on the item valueSince 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.
Font itemFont = new Font((String) value, Font.PLAIN, 24);
if (itemFont.canDisplayUpTo((String) value) == -1)The final step for this method is to return the modified renderer:
{
//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;Below is the complete source for the FontCellRenderer class:
import javax.swing.*;The final step is to set the renderer for the JList:
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;
}
}
listBox.setCellRenderer(new FontCellRenderer());
2 comments:
How I do the same, but using a JComboBox???
thanx boss
Post a Comment