Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a JMenuBar's font

I have problems with setting the font of a JMenuBar. I personally don't like the bold font Java frames use by default, so I tried to change it by using something like this:

public class MyFrame extends javax.swing.JFrame {
    public MyFrame() {
        JMenuBar menuBar = new JMenuBar();
        menuBar.setFont(new Font("sans-serif", Font.PLAIN, 12));
        setJMenuBar(menuBar);
        setSize(600, 400);

        // add some menus to the menu bar
        menuBar.add(new JMenu("Foo"));
        menuBar.add(new JMenu("Bar"));
        menuBar.add(new JMenu("Baz"));
        menuBar.add(new JMenu("Qux"));

        setVisible(true);
    }
}

As far as I know, the line menuBar.setFont(...) sets the font used by the component menuBar. But when I instantiated one of these frames, the default font didn't change at all, not even when I put the font's size to 30.

I appreciate any help concerning this.

like image 703
mezzodrinker Avatar asked Apr 07 '26 11:04

mezzodrinker


1 Answers

You can either try setting the font for each JMenu or change the default:

Font f = new Font("sans-serif", Font.PLAIN, 12);
UIManager.put("Menu.font", f);
like image 51
martinez314 Avatar answered Apr 09 '26 01:04

martinez314