Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

How to define a default font for JLabel or whole program?

Thread view: 
Chris - 15 Apr 2005 14:28 GMT
I don't really like how Java2 default font is bold.  I think the plain
looks much better.

How can I set the default font for a JLabel or for the whole program.
I would like to change it from BOLD to PLAIN.

About half of my JLabels in my program are anonymous.  It would quite a
pain to go and define a bunch of new JLabels, then change the font to
plain.
karlheinz klingbeil - 15 Apr 2005 14:41 GMT
Chris schrub am Freitag, 15. April 2005 15:28
folgendes:

> About half of my JLabels in my program are anonymous.
>  It would quite a pain to go and define a bunch of
> new JLabels, then change the font to plain.

I have made a set of functions which can set a couple
of fonts to all components and menubars. The following
codesnipped uses fonts which are instantiated in
another place, they are called listfont, buttonfont,
menufont and so on.
This snippets therefore can apply a certain font to all
listboxes, another to all buttons and labels....

Just call setFrameFonts(JFrame)
Hope this helps...

----------------------------snip--------------------

public void setFrameFonts(JFrame f){
       setComponentFonts(f.getContentPane());
       setMenuBar(f.getJMenuBar());
   }
   
   private void setMenuBar(JMenuBar mb){
       if (mb != null){
           for(int i = 0;i < mb.getMenuCount();i++){
               setMenuFont(mb.getMenu(i));
           }
       }
   }
   
   private void setMenuFont(JMenu m){
       m.setFont(MenuFont);
       for (int i = 0;i <
m.getMenuComponentCount();i++){
           m.getMenuComponent(i).setFont(MenuFont);    
           if (m.getMenuComponent(i) instanceof JMenu)
               setMenuFont((JMenu)m.getMenuComponent(i));
       }
   }
   
   private void setComponentFonts(Container c){
       for (int i=0;i<c.getComponentCount();i++){
           Component co = c.getComponent(i);
           if ((co instanceof JPanel) || (co
instanceof JTabbedPane) || (co instanceof JScrollPane)
|| (co instanceof JSplitPane) || ( co instanceof
JViewport)){
               setComponentFonts((Container)co);
           }
           else {
               if ((co instanceof JButton) || (co
instanceof JRadioButton))
                   co.setFont(ButtonFont);
               else {
                   if (co instanceof JList){
                      co.setFont(ListFont);  
                   }
                   else {
                       if ((co instanceof JEditorPane)
|| (co instanceof JTable) || (co instanceof
JTabbedPane) || (co instanceof JTextArea) || (co
instanceof JPasswordField) || (co instanceof
JTextField) || (co instanceof JTextPane))
                           co.setFont(TextFont);
                       else {
                           co.setFont(ButtonFont);
                       }
                    }          
               }
           }
       }
       if(c != null) {
               c.repaint();
       }
   }

----------------------------snip---------------------

Signature

greetz Karlheinz Klingbeil (lunqual)
http://www.lunqual.de oder http:www.lunqual.net

Knute Johnson - 16 Apr 2005 18:08 GMT
> I don't really like how Java2 default font is bold.  I think the plain
> looks much better.
[quoted text clipped - 5 lines]
> pain to go and define a bunch of new JLabels, then change the font to
> plain.

Chris:

Extend JLabel and set its font to PLAIN.

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

public class KLabel extends JLabel {
    public KLabel(String text) {
        super(text);

        Font font = getFont();
        setFont(font.deriveFont(Font.PLAIN));
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("KLabel Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new FlowLayout());

        KLabel k = new KLabel("Should Be Plain");
        f.add(k);

        JLabel j = new JLabel("Should Be Bold");
        f.add(j);

        f.pack();
        f.setVisible(true);
    }
}

Signature

Knute Johnson
email s/nospam/knute/

Chris - 17 Apr 2005 04:15 GMT
Exactly what I was looking for.  Thanks
Roland - 17 Apr 2005 09:54 GMT
> I don't really like how Java2 default font is bold.  I think the plain
> looks much better.
[quoted text clipped - 5 lines]
> pain to go and define a bunch of new JLabels, then change the font to
> plain.

Use UIManager to define JLabel's default font

import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;

public class LabelFont {

   public static void main(String[] args) {
      Font oldLabelFont = UIManager.getFont("Label.font");
      UIManager.put("Label.font", oldLabelFont.deriveFont(Font.PLAIN));

      JFrame f = new JFrame("LabelFont Test");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.getContentPane().setLayout(new FlowLayout());

      JLabel df = new JLabel("Default JLabel font");
      f.getContentPane().add(df);

      JLabel ef = new JLabel("Font explicitly set");
      ef.setFont(oldLabelFont);
      f.getContentPane().add(ef);

      f.pack();
      f.setVisible(true);
   }
}

Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \

Knute Johnson - 17 Apr 2005 19:06 GMT
>> I don't really like how Java2 default font is bold.  I think the plain
>> looks much better.
[quoted text clipped - 35 lines]
>    }
> }

Roland:

Where do you find a list of defaults that can be changed?

Thanks,

Signature

Knute Johnson
email s/nospam/knute/

Roland - 17 Apr 2005 19:36 GMT
[snip]

> Roland:
>
> Where do you find a list of defaults that can be changed?
>
> Thanks,

Here's a program that displays the UIManager's defaults:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;

public class UILister {
   public static void main(String[] args) {
      try {
         Set defaults = UIManager.getLookAndFeelDefaults().entrySet();
         TreeSet ts = new TreeSet(new Comparator() {
            public int compare(Object a, Object b) {
               Map.Entry ea = (Map.Entry) a;
               Map.Entry eb = (Map.Entry) b;
               return ((String) ea.getKey()).compareTo(((String)
eb.getKey()));
            }
         });
         ts.addAll(defaults);
         Object[][] kvPairs = new Object[defaults.size()][2];
         Object[] columnNames = new Object[] { "Key", "Value" };
         int row = 0;
         for (Iterator i = ts.iterator(); i.hasNext();) {
            Object o = i.next();
            Map.Entry entry = (Map.Entry) o;
            kvPairs[row][0] = entry.getKey();
            kvPairs[row][1] = entry.getValue();
            row++;
         }

         JTable table = new JTable(kvPairs, columnNames);
         JScrollPane tableScroll = new JScrollPane(table);

         JButton closeButton = new JButton("Close");
         closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               System.exit(0);
            }
         });

         JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER,
6, 6));
         buttons.add(closeButton, null);

         JPanel main = new JPanel(new BorderLayout());
         main.add(tableScroll, BorderLayout.CENTER);
         main.add(buttons, BorderLayout.SOUTH);

         JFrame frame = new JFrame("UI Properties");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.getContentPane().add(main);
         frame.pack();
         frame.setLocationRelativeTo(null);
         frame.setVisible(true);
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \

Knute Johnson - 17 Apr 2005 22:32 GMT
> [snip]
>
>> Roland:
>>
>> Where do you find a list of defaults that can be changed?

Thanks Roland!

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.