Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting Font at one place

Status
Not open for further replies.

niravpatel

Programmer
Dec 11, 2003
31
HK
Hi there,
In creating JFC/Swing Forms, how to setup the default JMenuBar, JMenu and JMenuItem Fonts so that I don't need to set the font individually. Point is, I want to set the font to Courier New for all the JMenuBar, JMenu and JMenuItems in one single place, how can I do that?

Thanking you,
Nirav
 
As with all Swing Components, there are UIManager properties that you can set to change their default look. In your case...

Font f = new Font(...);
UIManager.put("MenuBar.font", f);
UIManager.put("MenuItem.font", f);

...would probably get the job done. Keep in mind that these are just suggestions to the UI, a particular plaf could ignore these if it wanted to.

Hope this helps...
Josh Castagno
 
Hi Josh,
I tried as you said but It turned in to compilation error:
saying .font is not accessible!

Are you sure it works? Have you tried this before?
I am using Sun J2SDK 1.4.1...

Thanking You,
Nirav

Nirav Patel
#include<NoOneLivesForever>
 
I just tried this code on 1.4.2 and it worked fine... make sure your are setting the defaults before you create the menu stuf...

Code:
public class Test {
	public static final void main(String[] args) {
		Font f = new Font(&quot;courier&quot;, Font.BOLD, 20);

		UIManager.put(&quot;Menu.font&quot;, f);
		UIManager.put(&quot;MenuBar.font&quot;, f);
		UIManager.put(&quot;MenuItem.font&quot;, f);

		JFrame testframe = new JFrame(&quot;Test Window&quot;);
		testframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		testframe.getContentPane().setLayout(new BorderLayout(0, 0));

		JMenuBar mb = new JMenuBar();
		JMenu menu = new JMenu(&quot;Menu&quot;);
		menu.add(new JMenuItem(&quot;Menu Item 1&quot;));
		menu.add(new JMenuItem(&quot;Menu Item 2&quot;));
		mb.add(menu);

		testframe.setJMenuBar(mb);

		testframe.pack();
		testframe.setVisible(true);
	}
}
 
Thanks Josh,
It worked!

Nirav Patel
#include<NoOneLivesForever>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top