2 * jSite2 - FontList.java -
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.jsite.gui;
22 import java.awt.Component;
24 import java.awt.GraphicsEnvironment;
26 import javax.swing.DefaultComboBoxModel;
27 import javax.swing.JComboBox;
28 import javax.swing.JLabel;
29 import javax.swing.JList;
30 import javax.swing.ListCellRenderer;
33 * Combobox that displays a list of all available fonts, showing the name of the
34 * font in the font itself.
36 * @see GraphicsEnvironment#getAllFonts()
37 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
40 public class FontComboBox extends JComboBox {
42 /** The font names. */
43 private static final String[] fontNames;
46 Font[] allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
47 System.out.println("got " + allFonts.length + " fonts");
48 fontNames = new String[allFonts.length];
50 for (Font font: allFonts) {
51 fontNames[fontIndex++] = font.getName();
56 * Creates a new font combo box.
58 public FontComboBox() {
60 setModel(new DefaultComboBoxModel(fontNames));
61 setRenderer(new FontCellRenderer());
65 * The cell renderer for the font name cells.
67 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
70 private static class FontCellRenderer extends JLabel implements ListCellRenderer {
73 * Creates a new list cell renderer.
75 public FontCellRenderer() {
82 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
83 Font font = Font.decode((String) value);
85 setText((String) value);
86 setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
87 setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());