Reputation: 177
How would I use the GraphicsEnvironment.getAllFonts() method to populate a combo box with a list of all available fonts?
I used
JComboBox font = new
JComboBox(GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts());
But this didn't work.
Upvotes: 0
Views: 1924
Reputation: 285405
Regarding,
I used JComboBox font = new JComboBox(GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts());
That hasn't worked.
It does work. But you have to set the list cell renderer to display the font name. For example,
GraphicsEnvironment graphEnviron =
GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] allFonts = graphEnviron.getAllFonts();
JComboBox<Font> fontBox = new JComboBox<>(allFonts);
fontBox.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list,
Object value, int index, boolean isSelected, boolean cellHasFocus) {
if (value != null) {
Font font = (Font) value;
value = font.getName();
}
return super.getListCellRendererComponent(list, value, index,
isSelected, cellHasFocus);
}
});
JOptionPane.showMessageDialog(null, new JScrollPane(fontBox));
This is all well described in the combo box tutorial.
Upvotes: 2