Welcome, Guest. Please login or register.

Author Topic: JAVA global font setting  (Read 1753 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline elendilTopic starter

  • Sr. Member
  • ****
  • Join Date: Nov 2002
  • Posts: 324
    • Show only replies by elendil
    • http://www.idiot.fnuck.dk
JAVA global font setting
« on: May 20, 2004, 09:47:50 PM »
Hi.

I know this is not at all amiga related, but I've tried just about everything now (as well as searching everywhere).

I want to know if there is a way to set JAVA's default swing font globally instead of per J-item.

Failing that (I had already given up), does anyone know how I can set the font of a JDialog, if that's possible?
dialog.setFont(...) doesn't produce any errors, and a later getFont returns the font I've said, but it's not the font it uses to display its message.
Any ideas?

The problem is that it doesn't use a monospaced font for it's swing components, which makes aligning text a living nightmare, especially when you don't know the text to come.

Sincerely,

-Kenneth Straarup.

 

Offline toRus

  • Full Member
  • ***
  • Join Date: Mar 2003
  • Posts: 122
    • Show only replies by toRus
Re: JAVA global font setting
« Reply #1 on: May 21, 2004, 12:46:51 AM »
JDialog is a container. You should add a JLabel to its ContentPane. You are free to change the JLabel's font:
e.g.

Code: [Select]

import javax.swing.*;
import java.awt.*;

public class MyDialog
{
  public static void main(String[] args)
  {
    JFrame frame = new JFrame(); //owner        
    JDialog dialog = new JDialog(frame,"My Dialog");
    dialog.pack();
    dialog.setSize(320,240);    
    JLabel label = new JLabel("Hello world");
    label.setFont(new Font("MonoSpace",Font.ITALIC,20));
    dialog.getContentPane().add(label);    
    dialog.setVisible(true);
  }
}


Instead of adding the JLabel directly you could/should add it to a JPanel first, etc...


Regarding font global settings, the fonts are set by the LookAndFeel (L&F). You should create a custom LookAndFeel (or customise an existing one) class and define your preferred font settings for every item there using something like this e.g.

Code: [Select]

UIManager.put("Menu.font", new FontUIResource(yourFont));


(ditto for Button.font, Tree.font, CheckBox.font, etc.)


You can then use the created L&F in your code:
Code: [Select]

UIManager.setLookAndFeel(yourLookAndFeelClass);


or specify it at command line:
Code: [Select]

java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel YourApp


or change the contents (you might have to create it if it does not exist) of the file $JAVA_HOME/lib/swing.properties :
Code: [Select]

#swing.properties
swing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel


Alternatively, you might want to have a look at the contents of $JRE_HOME/lib/font.properties .

Look here,  here and here for more details.


 

Offline elendilTopic starter

  • Sr. Member
  • ****
  • Join Date: Nov 2002
  • Posts: 324
    • Show only replies by elendil
    • http://www.idiot.fnuck.dk
Re: JAVA global font setting
« Reply #2 on: May 21, 2004, 05:55:19 AM »
Yay, thanks a lot! I will try the look and feel approach first, but I have to go to work (buuh hiss!), so it won't be until later this evening.

Thanks a lot for the answer, again. You wouldn't want to know how much I have struggled with that.

Sincerely,

-Kenneth Straarup.
 

Offline elendilTopic starter

  • Sr. Member
  • ****
  • Join Date: Nov 2002
  • Posts: 324
    • Show only replies by elendil
    • http://www.idiot.fnuck.dk
Re: JAVA global font setting
« Reply #3 on: May 22, 2004, 12:37:34 AM »
Yay, proportional fonts all the way now.

THanks a lot.

I read through the link about how to modify and create your own look and feel classes, but didn't find it very well explained, except that it listed the various keys which helped a lot in doing the easy approach with UIManager.put(key, value);

I am rather excited about this look and feel thing, and have added such a custom class to my todo list.

Thanks again.

Sincerely,

-Kenneth Straarup.
 

Offline toRus

  • Full Member
  • ***
  • Join Date: Mar 2003
  • Posts: 122
    • Show only replies by toRus
Re: JAVA global font setting
« Reply #4 on: May 22, 2004, 01:30:09 PM »
:-)