2

Hello I have an odd question.

I want to know if and how to go about making universal settings for my Jlabels ,TextAreas and basically any text that can go on a panel. I have been writing the font sizes on every label to make it consistent. However the panels are growing and I don't want to hard code the font size on every label.

Here are the sizes I am using and would like to use on all my labels, and textarea's.

titlelb.getFont().deriveFont( Font.BOLD, (float) 20.); 
lb1.getFont().deriveFont( Font.PLAIN, (float) 11.);
lb2.getFont().deriveFont( Font.BOLD, (float) 11.);
textArea.setFont( Font.decode( "Arial-PLAIN-11"));

I am just wondering is there a way to set it universally where every Panel would inherit these specific sizes if that is at all possible, I don't really know how to go about it for the sizes of the labels which is why I am here asking. Thank you in advance.

M1M N7
  • 69
  • 2
  • 7
  • 2
    Why? I suspect you are doing something like [this](http://stackoverflow.com/a/12532237/230513), and no good will come of it. Even identically-named fonts have different metrics on different platforms. – trashgod Oct 18 '12 at 00:36
  • Why its a matter of consistency and because I am asked to do it lol,otherwise I could care less.And no i am certainly not doing anything like that. – M1M N7 Oct 19 '12 at 13:34

3 Answers3

7

You can change the font properties in the UIManager for every component you need by calling UIManager.put("nameOfFontProperty", new Font(Font.DIALOG, FONT.PLAIN, sizeInPixel));. You can find a list with the default properties here.

One example to make the font of all labels a size of 20px do:

UIManager.put("Label.font", UIManager.getFont("Label.font").deriveFont(20.0));
SwingUtilities.updateComponentTreeUI(yourJFrame);
halex
  • 16,253
  • 5
  • 58
  • 67
  • please to change nameOfFontProperty should be ComponentName.font, case sensitive – mKorbel Oct 17 '12 at 19:01
  • The best approach I've found is to perform this just after you've initalised the UIManager's look and feel and before you start the UI, although the `SwingUtilties#updateCompoent` should work just fine ;) – MadProgrammer Oct 17 '12 at 19:03
  • So i would add that line on every panel to make sure I get the font size set to 20, question lets say I use that. but there is one label i want set to 11, and i wrote leave the font set at 11. would it still leave it at 20 or will it be set to 11 as I would like? – M1M N7 Oct 19 '12 at 20:03
4

Write custom wrapper classes for any JComponent subclasses that you want to have a fixed font in your panels, and add the font code in the constructors. For example:

public class JLabelCustom extends JLabel {

    public JLabelCustom(...) {
        super();

        // Font setting code here
        .getFont().deriveFont( Font.BOLD, (float) 20.); 
        .getFont().deriveFont( Font.PLAIN, (float) 11.);
        .getFont().deriveFont( Font.BOLD, (float) 11.);
        this.setFont( Font.decode( "Arial-PLAIN-11"));
    }
}

The use the custom JComponent wherever you'd like the standardized font.

sampson-chen
  • 45,805
  • 12
  • 84
  • 81
  • Interesting, so what i would have to do is make a new class file and then write the code like the one above for reference.Then i can just extend it on every panel? – M1M N7 Oct 17 '12 at 18:52
  • It wouldn't be just every panel, it'd be any JComponent where you'd want a standardized font. – sampson-chen Oct 17 '12 at 18:55
  • right which is every Jlabel and text area lol. But I can just make a new class file and just extend it to my labels and such? – M1M N7 Oct 17 '12 at 19:36
  • okay, my other question is because i am worried on setting it all the same in this sense, For the title i want it to be 20 bold but for the labels that aren't the title I want them to be either 11 bold or plain. Would that be possible with this method? In terms of the text area i want it constantly Arial-PLAIN-11 so i could use it for the text area. but not so sure about the label. – M1M N7 Oct 17 '12 at 19:47
  • For any exceptions you want to the universal font, you can just manually set them after initialization to override the settings – sampson-chen Oct 17 '12 at 19:53
  • Oh Okay I see, thank you seems a little less work still some manual work here and there but slightly better – M1M N7 Oct 17 '12 at 20:38
1

You could make a subclass that automatically sets everything. Something like'

class UniversalJPanel extends JPanel{
    public UniversalJPanel(){
        super();
        this.setFont(...);
        }
    }

Then you could just use this instead of the regular JPanel. Same goes for JLabel, JTextArea etc

Perry
  • 98
  • 7
  • I kinda have something that already extends the JPanel a interface file. would it be possible to extend a class to JPANEL in addition to the interface file? And if so how would I do it? Just make a new class file similar to yours and then add it to the extend sentence? – M1M N7 Oct 17 '12 at 19:42
  • Is that interface a subclass of JPanel or just some settings file? If it's a settings file you could just extend JPanel no problem at all, since the settings will apply to the parent, and therefore will be inherited. – Perry Oct 17 '12 at 19:53
  • the interface file just has this ` import javax.swing.JPanel; public interface PanelValidate { public boolean Validate(); public JPanel getPanel(); public void SaveInfo(); }` Seems like a settings file but not 100 percent sure. – M1M N7 Oct 17 '12 at 20:41