RC said:
I knew there are JFileChooser and JColorChooser classes
from javax.swing.*
I am looking for a class "JFontChooser" list all available
system fonts and their font sizes.
Please tell me how to get the list of system fonts.
Thank Q very much in advance!
I had to write one a while back. It even has a main so you can try it out.
knute...
//
//
// JFontChooser
//
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JFontChooser extends JComponent {
private static final String alphabet =
"<html>abcdefghijklmnopqrstuvwxyz" +
"<br>ABCDEFGHIJKLMNOPQRSTUVWXYZ<br>0123456789.:,;
*!?')</html>";
private static JDialog dialog;
private static JComboBox fontBox;
private static JLabel fontLabel;
private static SpinnerNumberModel model;
private static JCheckBox boldCheckBox,italicCheckBox;
private static Font retcod;
public static Font showDialog(Component comp, Font font) {
JFrame frame = new JFrame();
dialog = new JDialog(frame,"Choose Font",true);
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
retcod = null;
}
});
if (font == null)
font = new Font("Dialog",Font.BOLD,12);
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String ac = ae.getActionCommand();
if (ac.equals("Font")) {
fontLabel.setFont(makeFont().deriveFont(16f));
} else if (ac.equals("Bold")) {
fontLabel.setFont(makeFont().deriveFont(16f));
} else if (ac.equals("Italic")) {
fontLabel.setFont(makeFont().deriveFont(16f));
} else if (ac.equals("OK")) {
retcod = makeFont();
dialog.setVisible(false);
} else if (ac.equals("Cancel")) {
retcod = null;
dialog.setVisible(false);
}
}
};
dialog.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = c.gridy = 0;
c.insets = new Insets(8,8,8,8);
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();
fontLabel = new JLabel(alphabet);
fontLabel.setPreferredSize(new Dimension(340,80));
fontLabel.setFont(font.deriveFont(16f));
dialog.add(fontLabel,c);
++c.gridy;
fontBox = new JComboBox(fonts);
fontBox.setSelectedItem(font.getFamily());
fontBox.setActionCommand("Font");
fontBox.addActionListener(al);
dialog.add(fontBox,c);
++c.gridy;
JPanel rowPanel = new JPanel(new
FlowLayout(FlowLayout.CENTER,16,0));
JPanel p = new JPanel();
model = new SpinnerNumberModel(font.getSize(),1,120,1);
model.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent ce) {
fontLabel.setFont(makeFont().deriveFont(16f));
}
});
JSpinner sizeSpinner = new JSpinner(model);
p.add(sizeSpinner);
JLabel l = new JLabel("Size");
p.add(l);
rowPanel.add(p);
boldCheckBox = new JCheckBox("Bold",font.isBold());
boldCheckBox.addActionListener(al);
rowPanel.add(boldCheckBox);
italicCheckBox = new JCheckBox("Italic", font.isItalic());
italicCheckBox.addActionListener(al);
rowPanel.add(italicCheckBox);
dialog.add(rowPanel,c);
++c.gridy;
p = new JPanel();
p.setLayout(new GridLayout(1,2,10,0));
JButton okButton = new JButton("OK");
okButton.addActionListener(al);
dialog.getRootPane().setDefaultButton(okButton);
p.add(okButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(al);
p.add(cancelButton);
dialog.add(p,c);
dialog.pack();
dialog.setLocationRelativeTo(comp);
dialog.setVisible(true);
dialog.dispose();
return retcod;
}
public static Font showDialog(Component comp) {
return showDialog(comp,comp.getFont());
}
private static Font makeFont() {
String name = (String)fontBox.getSelectedItem();
int style = (boldCheckBox.isSelected() ? Font.BOLD : Font.PLAIN) |
(italicCheckBox.isSelected() ? Font.ITALIC : Font.PLAIN);
int size = model.getNumber().intValue();
return new Font(name,style,size);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
final JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton b = new JButton("Open Dialog");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Font retcod =
JFontChooser.showDialog(b,b.getFont());
if (retcod != null)
b.setFont(retcod);
System.out.println(retcod);
}
});
frame.add(b);
frame.setSize(640,480);
frame.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}