Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7711501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:23:28+00:00 2026-06-01T01:23:28+00:00

im trying to change font globally for whole application, the problem is, i am

  • 0

im trying to change font globally for whole application, the problem is, i am able to do this only once. here is the code which helps you recreate the problem.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.awt.Font;
import java.util.Enumeration;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 *
 * @author osiris
 */
public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws InterruptedException {
        // TODO code application logic here
        JFrame frame = new JFrame();
        frame.setSize(600, 600);
        frame.setVisible(true);
        JButton button = new JButton("test");
        frame.add(button);
        frame.repaint();
        Thread.sleep(2000);
         Font font = new Font("Berlin Sans FB",java.awt.Font.PLAIN,14);
        Enumeration test = UIManager.getDefaults().keys();

        while ( test.hasMoreElements() ) {  

            Object key = test.nextElement();  
            Object value = UIManager.get( key );  
            if ( value instanceof Font ) {  
                UIManager.put( key, font );  
            }  
        }
        SwingUtilities.updateComponentTreeUI(frame);

        Thread.sleep(2000);
         Font font2 = new Font("Tempus Sans ITC",java.awt.Font.PLAIN,14);

         test = UIManager.getDefaults().keys();

        while ( test.hasMoreElements() ) {  

            Object key = test.nextElement();  
            Object value = UIManager.get( key );  
            if ( value instanceof Font ) {  
                UIManager.put( key, font2 );  
            }  
        }
        SwingUtilities.updateComponentTreeUI(frame);
    }
}

The font on the button is changed only once, why is that ? why it is not changed for the second time ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:23:29+00:00Added an answer on June 1, 2026 at 1:23 am

    Any/all changes to the already visible Container by set/change/modify value(s) to the UIManager/UIDeafaults is Look and Feel rellated issues, than you have to call

    SwingUtilities.updateComponentTreeUI(frame);
    

    EDIT if you want to update Font on runtime then you have to change for FontUIResource not simple Font

    enter image description hereenter image description hereenter image description here

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.plaf.FontUIResource;
    import javax.swing.plaf.basic.BasicComboBoxRenderer;
    
    public class SystemFontDisplayer extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private JFrame frame = new JFrame("Nimbus UIDeafaults and Font");
        private JComboBox fontsBox;
        private javax.swing.Timer timer = null;
        private JButton testButton = new JButton("testButton");
        private JTextField testTextField = new JTextField("testTextField");
        private JLabel testLabel = new JLabel("testLabel");
    
        public SystemFontDisplayer() {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            String[] fontFamilyNames = ge.getAvailableFontFamilyNames();
            fontsBox = new JComboBox(fontFamilyNames);
            fontsBox.setSelectedItem(0);
            fontsBox.setRenderer(new ComboRenderer(fontsBox));
            fontsBox.addItemListener(new ItemListener() {
    
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        final String fontName = fontsBox.getSelectedItem().toString();
                        fontsBox.setFont(new Font(fontName, Font.PLAIN, 16));
                        start();
                    }
                }
            });
            fontsBox.setSelectedItem(0);
            fontsBox.getEditor().selectAll();
            frame.setLayout(new GridLayout(4, 0, 20, 20));
            frame.add(fontsBox);
            frame.add(testButton);
            frame.add(testTextField);
            frame.add(testLabel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocation(200, 105);
            frame.pack();
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    fontsBox.setPopupVisible(true);
                    fontsBox.setPopupVisible(false);
                }
            });
            frame.setVisible(true);
        }
    
        private void start() {
            timer = new javax.swing.Timer(750, updateCol());
            timer.setRepeats(false);
            timer.start();
        }
    
        public Action updateCol() {
            return new AbstractAction("text load action") {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    final Font fnt = new Font(fontsBox.getSelectedItem().toString(), Font.PLAIN, 12);
                    final FontUIResource res = new FontUIResource(fnt);
                    UIManager.getLookAndFeelDefaults().put("Button.font", res);
                    UIManager.getLookAndFeelDefaults().put("TextField.font", res);
                    UIManager.getLookAndFeelDefaults().put("Label.font", res);
                    SwingUtilities.updateComponentTreeUI(frame);
                }
            };
        }
    
        public static void main(String arg[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    SystemFontDisplayer systemFontDisplayer = new SystemFontDisplayer();
                }
            });
        }
    
        private class ComboRenderer extends BasicComboBoxRenderer {
    
            private static final long serialVersionUID = 1L;
            private JComboBox comboBox;
            final DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
            private int row;
    
            private ComboRenderer(JComboBox fontsBox) {
                comboBox = fontsBox;
            }
    
            private void manItemInCombo() {
                if (comboBox.getItemCount() > 0) {
                    final Object comp = comboBox.getUI().getAccessibleChild(comboBox, 0);
                    if ((comp instanceof JPopupMenu)) {
                        final JList list = new JList(comboBox.getModel());
                        final JPopupMenu popup = (JPopupMenu) comp;
                        final JScrollPane scrollPane = (JScrollPane) popup.getComponent(0);
                        final JViewport viewport = scrollPane.getViewport();
                        final Rectangle rect = popup.getVisibleRect();
                        final Point pt = viewport.getViewPosition();
                        row = list.locationToIndex(pt);
                    }
                }
            }
    
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (list.getModel().getSize() > 0) {
                    manItemInCombo();
                }
                final JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, row, isSelected, cellHasFocus);
                final Object fntObj = value;
                final String fontFamilyName = (String) fntObj;
                setFont(new Font(fontFamilyName, Font.PLAIN, 16));
                return this;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to change the font of a NSButton subclass I've created. I'm able
I am trying to change the matplotlib font to helvetica, which I'd like to
i am trying to change the font on a pdftable and i have this
I am trying to do a application-wide font change and creating a style file
Iam trying to change the font size in String.xml Helloworld ..am able to do
I have the following code i am trying to change the font of a
I'm trying to change the font-size and color of a table cell, but it's
I am trying to change the font size using python's ImageDraw library. You can
I am trying to dynamically change the font and background color of my app
I'm trying to change the FontFamily of a ListBox to a fixed-width font, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.