how can I use font style for my whole frames or dialogs?

I have a main frame which has a menu bar and let user to change the font of all the texts in my program.I can just set the font for the main frame but how can i set that change for all my frames and dialogs???


You can use some king of UIManager for recording the font to use, then a Change Font action to update the font in UIManager before updating your UI Swing Component.

This thread has much more on the different possible solution on this topic.

The Swing application framework has a powerful Resource manager that allows you to customise fonts and other appearance settings via properties files. It has lots of flexibility allowing you to specify these kinds of settings per dialog. An introduction to the resource manager can be found here.

We simply do this to override the font for most of components we use.

    Font font = /*some font*/;

    UIManager.put("Button.font", font); //frozen
    UIManager.put("ToggleButton.font", font); //frozen
    UIManager.put("RadioButton.font", font); //frozen
    UIManager.put("CheckBox.font", font); //frozen
    UIManager.put("ColorChooser.font", font); //frozen
    UIManager.put("ComboBox.font", font); //frozen
    UIManager.put("Label.font", font); //frozen
    UIManager.put("List.font", font); //frozen
    UIManager.put("MenuBar.font", font); //frozen
    UIManager.put("MenuItem.font", font); //frozen
    UIManager.put("RadioButtonMenuItem.font", font); //frozen
    UIManager.put("CheckBoxMenuItem.font", font); //frozen
    UIManager.put("Menu.font", font); //frozen
    UIManager.put("PopupMenu.font", font); //frozen
    UIManager.put("OptionPane.font", font); //frozen
    UIManager.put("Panel.font", font); //frozen
    UIManager.put("ProgressBar.font", font); //frozen
    UIManager.put("ScrollPane.font", font); //frozen
    UIManager.put("Viewport.font", font); //frozen
    UIManager.put("TabbedPane.font", font); //frozen
    UIManager.put("Table.font", font); //frozen
    UIManager.put("TableHeader.font", font); //frozen
    UIManager.put("TextField.font", font); //frozen
    UIManager.put("PasswordField.font", font); //frozen
    UIManager.put("TextArea.font", font); //frozen
    UIManager.put("TextPane.font", font); //frozen
    UIManager.put("EditorPane.font", font); //frozen
    UIManager.put("TitledBorder.font", font); //frozen
    UIManager.put("ToolBar.font", font); //frozen
    UIManager.put("ToolTip.font", font); //frozen
    UIManager.put("Tree.font", font); //frozen
    UIManager.put("OptionPane.messsageFont", font); // frozen
    UIManager.put("OptionPane.buttonFont", font); //frozen

You can set the fonts for all UIManager elements which have a settable font with the code below.

UIManager.getFont(key) returns null if the element doesn't have a Font property.

    Font font = new Font("Arial", Font.PLAIN, 10);
    Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
    while (keys.hasMoreElements())
       Object key = keys.nextElement();
       if (UIManager.getFont(key) != null)
          UIManager.put(key, font);

However, the trick is to do this early in your application (e.g. literally the first line of your main file) or use SwingUtilities.updateComponentTreeUI(<your top component>); to update later.

Need Your Help

Java Exception Handling custom class

java exception custom-exceptions

I am having trouble handling some exceptions. It would be great if you could point me to a direction so I can understand exceptions better as well as learning to handle exceptions more efficient. T...