Results 1 to 9 of 9
- 07-02-2010, 04:00 PM #1
Can you set the font for an entire panel?
Up to now I have been setting each field individually
I tried setting it at the panel levelJava Code:aField.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)Java Code:myPanel.setFont (new Font("Courier", Font.PLAIN, 10));
- 07-02-2010, 04:09 PM #2
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.
- 07-02-2010, 04:18 PM #3
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.
- 07-02-2010, 05:17 PM #4
To see what the default font is, try calling getFont() for the component before setting the font.
- 07-02-2010, 05:36 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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(...);
- 07-05-2010, 07:10 AM #6
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);
-
Hm, Rob Camick's suggestion worked fine for me. Perhaps you were doing it wrong?
Also, seemed to work well for foreground color as well.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(); } }); } }Last edited by Fubarable; 07-05-2010 at 07:22 AM.
- 07-05-2010, 08:35 AM #8
That's a good idea. I will try it
- Thanks
- 07-05-2010, 10:45 AM #9
Similar Threads
-
Adding a panel to a panel
By rclausing in forum New To JavaReplies: 7Last Post: 02-02-2010, 05:56 AM -
Repaint the entire JFrame (GroupLayout)
By Willi in forum AWT / SwingReplies: 13Last Post: 12-19-2009, 10:11 PM -
How to change font/ font color etc in a graphic object using JCombobox?
By JavaInLove in forum AWT / SwingReplies: 5Last Post: 04-25-2009, 08:00 PM -
Download the entire web site?
By makpandian in forum New To JavaReplies: 0Last Post: 03-14-2009, 10:39 AM -
How to synchronize blocks instead of entire methods
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks