Home > Archive > Java Help > January 2006 > Is there JFontChooser
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Is there JFontChooser
|
|
|
| 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!
| |
| mail.crmurali@gmail.com 2006-01-24, 7:05 pm |
| Refer the Font2DTest demo source that comes along with the J2SDK.
| |
| Fahd Shariff 2006-01-24, 7:05 pm |
| You may want to see:
http://www.geocities.com/fahdsharif...fontshower.html (source
included)
"It will show you what fonts are available via Java on your machine,
and what they look like in a variety of styles, sizes and colours."
Hope this helps.
--
Fahd Shariff
| |
| Knute Johnson 2006-01-24, 7:06 pm |
| 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/
| |
| Roedy Green 2006-01-24, 7:06 pm |
| On Tue, 24 Jan 2006 08:31:23 -0500, RC <raymond.chui@nospam.noaa.gov>
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.
| |
|
|
| IchBin 2006-01-24, 7:06 pm |
| 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/show...ackage_id=16653
or
http://cvs.sourceforge.net/viewcvs....rg/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-)
| |
| Roedy Green 2006-01-24, 7:06 pm |
| On Tue, 24 Jan 2006 17:17:49 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> 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.
|
|
|
|
|