Results 1 to 9 of 9
  1. #1
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default Can you set the font for an entire panel?

    Up to now I have been setting each field individually

    Java Code:
    aField.setFont (new Font("Courier", Font.PLAIN, 10));
    I tried setting it at the panel level

    Java Code:
    myPanel.setFont (new Font("Courier", Font.PLAIN, 10));
    It compiles, but the font remains unchanged. Is there a way to change the default font setting for a panel so that I can then only change a few fields to a different font, rather than setting dozens of fields to Courier? (I have only two fields out of 30 that I want at a non-Courier font)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Where does each field get its font? If there is a central location, that would be the place to put the desired font. If each field saves it own font, then each will have to be changed.
    Make a list of all the fields and use a loop to set the fonts for each.

  3. #3
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    That was, basically, my question - where is the default font set? I don't set it in my app, I change the font on each field individually. I would have guessed that it is set at the panel level, especially since it allowed the compile, but that doesn't seem to be the case (or I am setting the panel font at the wrong place). I've read the Java docs on this, and I cannot find anything that addresses a default font.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    To see what the default font is, try calling getFont() for the component before setting the font.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    If a component has a null Font then I believe it uses the Font of its parent.
    Java Code:
    textField.setFont(null);
    panel.add( textField );
    panel.setFont(...);

  6. #6
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    No setting component font to null won't make it acquire font of its parent. It didn't work with me.

    To make font setting little easy do like :
    Java Code:
    Font f1 = new Font("Arial", Font.BOLD, 20);
    buttonNew.setFont(f1);
    buttonClick.setFont(f1);

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Prajin View Post
    No setting component font to null won't make it acquire font of its parent. It didn't work with me.
    Hm, Rob Camick's suggestion worked fine for me. Perhaps you were doing it wrong?

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class FontTest {
    
      private static final int LABEL_COUNT = 5;
    
      private static void createAndShowUI() {
        JPanel panel = new JPanel(new GridLayout(1, 0, 30, 0));
        panel.setFont(new Font(Font.SERIF, Font.BOLD, 64));
        panel.setForeground(Color.red);
        
        JLabel[] labels = new JLabel[LABEL_COUNT];
        for (JLabel label : labels) {
          label = new JLabel("Foo");
          label.setFont(null);
          label.setForeground(null);
          panel.add(label);
        }
        
        
        JFrame frame = new JFrame("FontTest");
        frame.getContentPane().add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }
    Also, seemed to work well for foreground color as well.
    Last edited by Fubarable; 07-05-2010 at 07:22 AM.

  8. #8
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    That's a good idea. I will try it
    - Thanks

  9. #9
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Worked for me as well, thanks Rob. The key was setting the JLabel to null. Unfortunately, that's not any different than what I was doing, setting the font on each element separately. Apparently there is no way to cleanly set the font for the entire panel.

Similar Threads

  1. Adding a panel to a panel
    By rclausing in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 05:56 AM
  2. Repaint the entire JFrame (GroupLayout)
    By Willi in forum AWT / Swing
    Replies: 13
    Last Post: 12-19-2009, 10:11 PM
  3. Replies: 5
    Last Post: 04-25-2009, 08:00 PM
  4. Download the entire web site?
    By makpandian in forum New To Java
    Replies: 0
    Last Post: 03-14-2009, 10:39 AM
  5. Replies: 0
    Last Post: 04-09-2008, 06:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •