0

I'm trying to make the menu bar items and the items of the items bigger and smaller, I've seached here a bit before (so don't mark this as repeated please) and none of the posts I've found are working.

I've tried the following codes without success:

Font f = new Font("sans-serif", Font.PLAIN, 12);
UIManager.put("menuBar.font", f);

And

menuBar.setFont(new Font(menuBar.getFont().getFontName(), menuBar.getFont().getStyle(), 12));

And this is my code where I'm trying to edit the font:

private class foo{
        private JMenu mnArchivo;
        private JMenuBar menuBar;
        menuBar = new JMenuBar();
        frmAdministracinHospital.setJMenuBar(menuBar);

    JRadioButtonMenuItem rdbtnmntmGrande = new JRadioButtonMenuItem("Grande");
            rdbtnmntmGrande.addActionListener(new MiGrandeActionListener());
            rdbtnmntmGrande.setIcon(new ImageIcon(PrincipalWindow.class.getResource("/presentacion/fontbig.png")));
            buttonGroup.add(rdbtnmntmGrande);
            mnTamanoFuente.add(rdbtnmntmGrande);

    private class MiGrandeActionListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {

                Font f = new Font(menuBar.getFont().getFontName(), menuBar.getFont().getStyle(), 12);
                UIManager.put("Menu.font", f);
            }
        }

Any clue please?

Ulises CT
  • 1,361
  • 1
  • 12
  • 21

2 Answers2

4

That's because there's not "menuBar.font" key in the UIManager class, it should be:

UIManager.put("MenuBar.font", f);

i.e. Caps are important or:

UIManager.put("MenuItem.font", f);

for each JMenuItem's font

Here's a list of these properties

Also related: Changing a JMenuBar's font


EDIT: Added mcve

I don't see where it's not working, if I run this code, it works fine for me

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class UIManagerFontChangeExample {
    
    private JFrame frame;
    private JLabel label;
    private JMenuItem item1, item2;
    private JMenu menu;
    private JMenuBar bar;
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Font f = new Font("sans-serif", Font.PLAIN, 12);
                UIManager.put("Menu.font", f);
                UIManager.put("MenuItem.font", f);
                UIManagerFontChangeExample example = new UIManagerFontChangeExample();
                example.createAndShowGui();
            }
        });
    }
    
    public void createAndShowGui() {
        frame = new JFrame("Font changing example");
        label = new JLabel("This is a label");
        bar = new JMenuBar();
        menu = new JMenu("Menu");
        item1 = new JMenuItem("Item1");
        item2 = new JMenuItem("Item2");
        
        menu.add(item1);
        menu.add(item2);
        bar.add(menu);
        
        frame.add(label);
        frame.setJMenuBar(bar);
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

Before changing font:

enter image description here

After changing font:

enter image description here

The code I added above is called: Minimal, Complete and Verifiable Example, next time, please post a valid one, the same I did, so we can copy-paste, I didn't use an actionListener because the question isn't related to the actions, but the font. Or Icon because it's not related either, I did a full example code that you can copy-paste and see how it works, without modifying anything, that's what you were asked for.

For the MenuBar font you need to call:

UIManager.put("Menu.font", f);

And for the MenuItem

UIManager.put("MenuItem.font", f);
Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • still doesn't work, isn't it supposed to be the name of my menu bar component, which is menuBar? – Ulises CT Dec 27 '16 at 19:51
  • It's not the name of your component, it's the name of the property. It will change **all** of your `JMenuBar`, I suggest you to check with `MenuItem` and use a bigger font to check it, if it's still not working, please provide a valid [mcve] – Frakcool Dec 27 '16 at 19:53
  • still the same :/ , idk if it's supposed to work with JLabel also, but I tried that too and doesn't work too. The only code I have is the declaration of the components and the listener, do i still add it? – Ulises CT Dec 27 '16 at 19:58
  • Please read what a [mcve] is, i.e. a small program which has 1 `JMenu`, with 1 or 2 `JMenuItems` and a `JLabel` probably, where you show how you're changing font – Frakcool Dec 27 '16 at 20:01
  • but what I really wanna change is the font size – Ulises CT Dec 27 '16 at 20:19
  • okay I've just realized what's wrong, if I put that code since the very begining it works, but I want to change the size when pressing an option from a menu item and there is when it doesn't work, if I put it in its listener @Frakcool – Ulises CT Dec 28 '16 at 09:28
  • @UlisesCT this question never said it had to be on run time, this question was solved already, please ask a new question including a [mcve] and accept the answer. But please, first search [similar questions](http://stackoverflow.com/a/2315056/2180785) because they have been answered already – Frakcool Dec 28 '16 at 13:12
  • Not sure if you've solved this already, but if you want to make changes to UI Manager properties on the fly, you'll need to use the SwingUtilities.updateComponentTreeUI(Component) method and pass in your highest level component, usually the Window or Frame you component is contained in. – jdb1015 Jul 13 '17 at 21:01
  • @jdb1015 the question you're referring to [has already been solved](https://stackoverflow.com/q/41364080/2180785) it was a question by the same user – Frakcool Jul 13 '17 at 22:38
0

If you're updating UI Manager properties on the fly (such as after your GUI has appeared), you'll need to make a call to SwingUtilities.updateComponentTreeUI(Component) after you set the property UI Manager property. You'll want to pass in the highest level component you want to refresh (in this case, probably your top level Window or JFrame).

jdb1015
  • 127
  • 6