Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Change font style to Bold, Swing, all components

 
yoga nina
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to code menu buttons to change the font style between Normal and Bold. The only sample code in my text only references one label whereas I have to code it so all the components, text, dialog, numbers etc change when button selected. The code is:
if (mnuBold.isSelected()) {
java.awt.Font oldFont = lblOutput.getFont();
java.awt.Font newFont = new java.awt.Font(oldFont.getName(), java.awt.Font.BOLD, oldFont.getSize()):
lblOutput.setFont(newFont);
}

How can I change this code so that it will apply to all the components and not just the lblOutput?
Is there better code to accomplish this?
Thanks for your help.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I change this code so that it will apply to all the components and not just the lblOutput?

Iterate recursively over all the components in the top level container. Or you could use that appropriate utility method of my SwingUtils class tp get a List of all children that are JComponent subclasses.

Is there better code to accomplish this?

Also, use imports so you don't have to clutter up the code with fully qualified class names.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic