Is there JFontChooser

Discussion in 'Java' started by RC, Jan 24, 2006.

  1. RC

    RC Guest

    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!
    RC, Jan 24, 2006
    #1
    1. Advertising

  2. RC wrote:
    > 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.


    String fontNames[] = GraphicsEnvironment.
    getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

    (more info in the API doc of class java.awt.GraphicsEnvironment)

    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
    Thomas Fritsch, Jan 24, 2006
    #2
    1. Advertising

  3. RC

    Guest

    Refer the Font2DTest demo source that comes along with the J2SDK.
    , Jan 24, 2006
    #3
  4. RC

    Fahd Shariff Guest

    Fahd Shariff, Jan 24, 2006
    #4
  5. RC wrote:
    > 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);
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Jan 24, 2006
    #5
  6. RC

    Roedy Green Guest

    On Tue, 24 Jan 2006 08:31:23 -0500, RC <>
    wrote, quoted or indirectly quoted someone who said :

    >I am looking for a class "JFontChooser" list all available
    >system fonts and their font sizes.


    Fonts come in all sizes in Java. See
    http://mindprod.com/applets/fontshower.html
    and
    http://mindprod.com/applets/fontshowerawt.html

    for source code to display a list of all available fonts.

    A fancier version would give you a preview of what each font looked
    like. That would be high overhead. You would have to load and
    instantiate every font in existence. You might do that by caching an
    image for each font.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 24, 2006
    #6
  7. RC

    Roedy Green Guest

    Roedy Green, Jan 24, 2006
    #7
  8. RC

    IchBin Guest

    RC wrote:
    > 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 wrote one for the HSQLDB product and it is called "FontDialogSwing".
    You can find it in the Download of current HSQLDB source or online in CVS.
    http://sourceforge.net/project/showfiles.php?group_id=23316&package_id=16653
    or
    http://cvs.sourceforge.net/viewcvs.py/hsqldb/hsqldb-dev/src/org/hsqldb/util/

    It's located in package org.hsqldb.util.FontDialogSwing;

    It originally used a spinner for the font size but commented it out and
    used something else for product compatibility to older versions of JVM.
    --

    Thanks in Advance...
    IchBin, Pocono Lake, Pa, USA
    http://weconsultants.servebeer.com/JHackerAppManager
    __________________________________________________________________________

    'If there is one, Knowledge is the "Fountain of Youth"'
    -William E. Taylor, Regular Guy (1952-)
    IchBin, Jan 24, 2006
    #8
  9. RC

    Roedy Green Guest

    On Tue, 24 Jan 2006 17:17:49 GMT, Roedy Green
    <> wrote, quoted or
    indirectly quoted someone who said :

    >A fancier version would give you a preview of what each font looked
    >like. That would be high overhead.


    By that I meant on the menu, the way MS Word does.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 24, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CM
    Replies:
    2
    Views:
    3,090
    James Zhuo
    Aug 13, 2003
  2. Danno
    Replies:
    2
    Views:
    1,070
    Danno
    Apr 26, 2006
  3. David Stockwell
    Replies:
    2
    Views:
    534
    Grant Edwards
    Jun 8, 2004
  4. Eric Danstron

    Are there ANY good forums out there anymore???

    Eric Danstron, Sep 21, 2005, in forum: ASP General
    Replies:
    1
    Views:
    135
    Immediacy
    Sep 22, 2005
  5. Gennady
    Replies:
    0
    Views:
    81
    Gennady
    Jul 31, 2003
Loading...

Share This Page