A suo tempo mi ero fatto un componente apposito, un po' in stile Font Chooser di Word. Penso che in giro ce ne siano implementazioni migliori, però lo posto lo stesso, magari qualcuno si passa il tempo a sistemarlo.
codice:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Andrea
*/
public class JFontChooser extends JComboBox {
private GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
private String[] fontFamilies = ge.getAvailableFontFamilyNames();
private Font[] myFont;
private class MyComboBoxModel extends DefaultComboBoxModel {
public MyComboBoxModel() {
super(myFont);
}
}
public void actionPerformed(ActionEvent ae) {
this.setFont((Font)this.getSelectedItem());
this.validate();
}
public JFontChooser() {
this(12, Color.WHITE, Color.BLACK, Color.BLUE, 7);
}
/** Creates a new instance of JFontChooser */
public JFontChooser(int textSize, Color bgColor, Color selColor, Color hvrColor, int listSize) {
super();
final Color selectionColor = selColor;
final Color backgroundColor = bgColor;
final Color hoverColor = hvrColor;
myFont = new Font[fontFamilies.length];
for (int i=0; i < myFont.length; i++) {
myFont[i] = new Font(fontFamilies[i], Font.PLAIN, textSize);
}
this.setModel(new MyComboBoxModel());
this.setBackground(backgroundColor);
this.setMaximumRowCount(listSize);
this.setRenderer (new ListCellRenderer () {
public Component getListCellRendererComponent (JList list, Object value, int index, boolean selected, boolean cellHasFocus) {
Font font = (Font) value;
JLabel label = new JLabel (" "+font.getName());
label.setFont(font);
label.setForeground (selected ? hoverColor : selectionColor);
return label;
}
});
this.addActionListener(this);
}
/*public static void main (String[] args) {
JFrame jf = new JFrame("test dei font");
jf.setSize(200,200);
jf.getContentPane().add(new JFontChooser());
jf.setVisible(true);
}*/
}