Create a font chooser in Java

Description

The following code shows how to create a font chooser.

Example


   /*w  ww.ja  v a  2s . c o m*/

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main{
  public static void main(String[] args) {
    new FontStyleSizeFamilyChooser();
  }

}
class FontStyleSizeFamilyChooser extends JFrame {

  private JLabel sampleText = new JLabel("Label");

  private JComboBox fontComboBox;

  private JComboBox sizeComboBox;

  private JCheckBox boldCheck, italCheck;

  private String[] fonts;

  public FontStyleSizeFamilyChooser() {
    this.setSize(500, 150);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    FontListener fl = new FontListener();
    this.add(sampleText, BorderLayout.NORTH);
    GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
    fonts = g.getAvailableFontFamilyNames();

    JPanel controlPanel = new JPanel();
    fontComboBox = new JComboBox(fonts);
    fontComboBox.addActionListener(fl);
    controlPanel.add(new JLabel("Family: "));
    controlPanel.add(fontComboBox);

    Integer[] sizes = { 7, 8, 9, 10, 11, 12, 14, 18, 20, 22, 24, 36 };

    sizeComboBox = new JComboBox(sizes);
    sizeComboBox.setSelectedIndex(5);
    sizeComboBox.addActionListener(fl);
    controlPanel.add(new JLabel("Size: "));
    controlPanel.add(sizeComboBox);

    boldCheck = new JCheckBox("Bold");
    boldCheck.addActionListener(fl);
    controlPanel.add(boldCheck);

    italCheck = new JCheckBox("Ital");
    italCheck.addActionListener(fl);
    controlPanel.add(italCheck);

    this.add(controlPanel, BorderLayout.SOUTH);
    fl.updateText();

    this.setVisible(true);
  }

  private class FontListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      updateText();
    }

    public void updateText() {
      String name = (String) fontComboBox.getSelectedItem();

      Integer size = (Integer) sizeComboBox.getSelectedItem();

      int style;
      if (boldCheck.isSelected() && italCheck.isSelected())
        style = Font.BOLD | Font.ITALIC;
      else if (boldCheck.isSelected())
        style = Font.BOLD;
      else if (italCheck.isSelected())
        style = Font.ITALIC;
      else
        style = Font.PLAIN;

      Font f = new Font(name, style, size.intValue());
      sampleText.setFont(f);
    }
  }
}

The code above generates the following result.

Create a font chooser in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer