Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Is there JFontChooser

Reply
Thread Tools

Is there JFontChooser

 
 
RC
Guest
Posts: n/a
 
      01-24-2006
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!
 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      01-24-2006
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().getAvailableFontFami lyNames();

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

--
"Thomas:Fritsch$ops:de".replace(':','.').replace(' $','@')
 
Reply With Quote
 
 
 
 
mail.crmurali@gmail.com
Guest
Posts: n/a
 
      01-24-2006
Refer the Font2DTest demo source that comes along with the J2SDK.

 
Reply With Quote
 
Fahd Shariff
Guest
Posts: n/a
 
      01-24-2006
You may want to see:

http://www.geocities.com/fahdshariff...ontshower.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

 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      01-24-2006
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,;

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_CLOS E);
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/
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-24-2006
On Tue, 24 Jan 2006 08:31:23 -0500, RC <(E-Mail Removed)>
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-24-2006
On 24 Jan 2006 08:01:23 -0800, "Fahd Shariff" <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>http://www.geocities.com/fahdshariff...ontshower.html (source
>included)


your link to my site should be
http://mindprod.com/applets/fontshower.html

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
IchBin
Guest
Posts: n/a
 
      01-24-2006
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/showf...ckage_id=16653
or
http://cvs.sourceforge.net/viewcvs.p...g/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-)
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-24-2006
On Tue, 24 Jan 2006 17:17:49 GMT, Roedy Green
<(E-Mail Removed) > 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a java.util.Scanner that works in 1.4.2 out there? Danno Java 2 04-26-2006 04:41 AM
Are there any Belkin router users out there? OM UK VOIP 12 10-04-2005 08:32 AM
is there a hardware / driver combination out there the lets win2k connect to the network before login?? christiane kewitz Wireless Networking 1 02-13-2005 01:08 AM
I am getting loads of spam by e-mail.Most of it is not even addressed to me so god knows how I am receiving it. Any ideas . Is there a spam guard available on blueyonder? How do I get it on my system if there is?Cheers GW Geoff/Elaine Computer Support 11 11-16-2004 11:17 PM
Are there any free .pdf writers out there Goonigoogoo Computer Support 5 11-22-2003 03:27 AM



Advertisments