Font Dialog Box....Program code Inside !! Only Font Name not working !!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
publicclass home2
{
Panel p1, p2, p3, pb,tb, main_panel;
final List li1,li2,li3;
final TextField t1, t2, t3;
Label l1;
public home2()
{
p1 =new Panel();
p2 =new Panel();
p3 =new Panel();
pb =new Panel();
tb=new Panel();
main_panel =new Panel();
Frame f =new Frame("Font Dialog");
f.addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
});
t1 =new TextField(25);
li1 =new List(7,false);
li1.addItemListener(new ItemListener(){
publicvoid itemStateChanged(ItemEvent e){
String str = li1.getSelectedItem();
t1.setText(str);
}
});
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontlist[] = ge.getAvailableFontFamilyNames();
for(int i=0;i!=fontlist.length;i++)
{
li1.add(fontlist[i]);
}
t2 =new TextField(15);
li2 =new List(7,false);
li2.addItemListener(new ItemListener(){
publicvoid itemStateChanged(ItemEvent e){
String str = li2.getSelectedItem();
t2.setText(str);
}
});
li2.add("Regular");
li2.add("Italic");
li2.add("Bold");
li2.add("Bold Italic");
t3 =new TextField(15);
li3 =new List(7,false);
li3.addItemListener(new ItemListener(){
publicvoid itemStateChanged(ItemEvent e){
String str = li3.getSelectedItem();
t3.setText(str);
}
});
for(int i=0;i!=72;i++)
{
li3.add(String.valueOf(i));
}
Button b1 =new Button("OK");
b1.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
deliver();
}
});
Button b2 =new Button("Cancel");
l1 =new Label("SAMPLE");
l1.setSize(600,100);
l1.setLocation(0,0);
p1.add(li1);
p1.add(li2);
p1.add(li3);
p2.add(b1);
p2.add(b2);
p3.add(l1);
tb.add(t1);
tb.add(t2);
tb.add(t3);
pb.add(tb);
pb.add(p1);
pb.add(p2);
pb.setLayout(new FlowLayout(FlowLayout.LEFT));
p3.setLayout(null);
main_panel.add(pb);
main_panel.add(p3);
main_panel.setLayout(new BoxLayout(main_panel, BoxLayout.Y_AXIS));
f.add(main_panel);
f.setSize(600,300);
f.setVisible(true);
}
void deliver()
{
String name = li1.getSelectedItem();
System.out.println(name);
String style = li2.getSelectedItem();
String size = li3.getSelectedItem();
int i=0;
if(style.equalsIgnoreCase("regular"))
{
i=0;
}
elseif(style.equalsIgnoreCase("bold"))
{
i =1;
}
elseif(style.equalsIgnoreCase("italic"))
{
i=2;
}
elseif(style.equalsIgnoreCase("bold italic"))
{
i=3;
}
Font f =new Font(name,i,Integer.parseInt(size));
l1.setFont(f);
}
publicstaticvoid main(String args[])
{
new home2();
}
}
You are free to Modify the Code in Every way possible.... just tell me where i am going wrong with Font Name .... why it is not working !!!
# 1
What's the matter with it ... it works fine!
# 2
No its not....Select this font "Monotype Corsiva" or any other that u are aware of ....and then select the size and click "OK" !!The Font Style is not Actually showing up on the Label Control !!
# 3
I beg to differ ... it is for me.
# 4
Not working..... here's the proof !! http://img15.imgspot.com/?u=/u/07/67/12/see1173460571.gif
# 5
My apologies ... yes you are right - I was not looking carefully enough. Only a few of them work (Dialog, Serif, SansSerif, Monospaced) to name just a few.
This from the API doco for java.awt.Font:
Physical and Logical Fonts
The Java Platform distinguishes between two kinds of fonts: physical fonts
and logical fonts.
Physical fonts are the actual font libraries containing glyph data and tables to
map from character sequences to glyph sequences, using a font technology
such as TrueType or PostScript Type 1. All implementations of the Java
Platform must support TrueType fonts; support for other font technologies is
implementation dependent. Physical fonts may use names such as
Helvetica, Palatino, HonMincho, or any number of other font names. Typically,
each physical font supports only a limited set of writing systems, for
example, only Latin characters or only Japanese and Basic Latin. The set of
available physical fonts varies between configurations. Applications that
require specific fonts can bundle them and instantiate them using the
createFont method.
Logical fonts are the five font families defined by the Java platform which
must be supported by any Java runtime environment: Serif, SansSerif,
Monospaced, Dialog, and DialogInput. These logical fonts are not actual font
libraries. Instead, the logical font names are mapped to physical fonts by the
Java runtime environment. The mapping is implementation and usually
locale dependent, so the look and the metrics provided by them vary.
Typically, each logical font name maps to several physical fonts in order to
cover a large range of characters.
I suggest to go there and read up ...
~Bill
PS: It adds: Peered AWT components, such as Label and TextField, can only use logical fonts.
# 6
****.... this is ridiculous.Any Way to solve this problem ?
# 7
If you look at the API DOCS ... I was looking at version 1.6 ... it says that asof v1.5x there is a createFont method for True Type Fonts. You need to pass in the final int value TRUETYPE_FONT and a File Object represinting that font. This is the first time I am looking at it, and as yet I did not get the chance to really get into it. My advice? ...
1. Read through this carefully
2. Post to the Swing forum
3. Google for Already made code.
Sorry I can't be more help.
~Bill
PS: Have a read ... [url http://mindprod.com/jgloss/font.html]Java Glossary[/url]
[url http://www.javaworld.com/javaworld/javatips/jw-javatip81.html]Java Tip 81: Jazz up the standard Java fonts[/ur]
# 8
Thanks for your help. I Will look for it and post here if i find the solution.
# 9
It's really not all that bad ... it caught my interest and so I read that first link. Then I re-pgm'd your code to swing - here have a look:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FunnyNameForGUI
{
JPanel p1, p2, p3, pb,tb, main_panel;
final List li1,li2,li3;
final JTextField t1, t2, t3;
JLabel l1;
public FunnyNameForGUI()
{
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
pb = new JPanel();
tb= new JPanel();
main_panel = new JPanel();
JFrame f = new JFrame("Font Dialog");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
t1 = new JTextField(25);
li1 = new List(7, false);
li1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e){
String str = li1.getSelectedItem();
t1.setText(str);
}
});
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontlist[] = ge.getAvailableFontFamilyNames();
for(int i=0;i!=fontlist.length;i++)
{
li1.add(fontlist[i]);
}
t2 = new JTextField(15);
li2 = new List(7, false);
li2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e){
String str = li2.getSelectedItem();
t2.setText(str);
}
});
li2.add("Regular");
li2.add("Italic");
li2.add("Bold");
li2.add("Bold Italic");
t3 = new JTextField(15);
li3 = new List(7, false);
li3.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e){
String str = li3.getSelectedItem();
t3.setText(str);
}
});
for(int i=0;i!=72;i++)
{
li3.add(String.valueOf(i));
}
JButton b1 = new JButton("OK");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
deliver();
}
});
JButton b2 = new JButton("Cancel");
l1 = new JLabel("SAMPLE");
l1.setSize(600,100);
l1.setLocation(0,0);
p1.add(li1);
p1.add(li2);
p1.add(li3);
p2.add(b1);
p2.add(b2);
p3.add(l1);
tb.add(t1);
tb.add(t2);
tb.add(t3);
pb.add(tb);
pb.add(p1);
pb.add(p2);
pb.setLayout(new FlowLayout(FlowLayout.LEFT));
p3.setLayout(null);
main_panel.add(pb);
main_panel.add(p3);
main_panel.setLayout(new BoxLayout(main_panel, BoxLayout.Y_AXIS));
f.getContentPane().add(main_panel);
f.setSize(600,300);
f.setVisible(true);
}
void deliver()
{
String name = li1.getSelectedItem();
System.out.println(name);
String style = li2.getSelectedItem();
String size = li3.getSelectedItem();
int i=0;
if(style.equalsIgnoreCase("regular"))
{
i=0;
}
else if(style.equalsIgnoreCase("bold"))
{
i =1;
}
else if(style.equalsIgnoreCase("italic"))
{
i=2;
}
else if(style.equalsIgnoreCase("bold italic"))
{
i=3;
}
System.out.println("name: "+name);
Font f = new Font(name,i,Integer.parseInt(size));
l1.setFont(f);
}
public static void main(String args[])
{
new FunnyNameForGUI();
}
}
Run it ... in the words of some clothier ... "I think you're gonna like what you see" ... not to be presumptuous ;o)
# 10
But the question still remains unanswered if the Font class had all the problems that u mentioned and only few fonts were actually usable why is working with swing and not with AWT ?
# 11
The question really does not remain if you read the links I believe it explains most of it. That really was not your original question though - you said you wanted to get it fixed. I spent time helping you to do that and I think I deserve more than additional questions in return - don't
# 12
That's the thanks we get for trying to help. Just great.