Is there JFontChooser

R

RC

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!
 
T

Thomas Fritsch

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.

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

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

Knute Johnson

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);
}
}
 
R

Roedy Green

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.
 
I

IchBin

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 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-)
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top