Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using custom fonts on MenuBar Menu and/or MenuItem

921851Mar 13 2012 — edited Mar 14 2012
Hi all!

I was wondering if there's a way to set a custom font to a Menu and it's children. I can actually do this on the Text object like:
...

import javafx.scene.text.Font;
import javafx.scene.text.Text;

...

@FXML private Text lblAppName, lblLoginFailed;

...

Font myFont = Font.loadFont(getClass().getResourceAsStream("/fonts/TRON.TTF"), 40);
	
if(myFont != null)
 lblAppName.setFont(myFont);
But can't do it on the javafx.scene.control.Menu and javafx.scene.control.MenuItem objects.

I even tried the register font approach, even knowing that javafx.scene.text.Font and java.awt.Font are two different objects and got no success at all. As follows
try 
{
	GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
	ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/fonts/Baumans-Regular.ttf")));
} 
catch (Exception e) 
{
	e.printStackTrace();
}
and then tried to use it from my css file:
.menu-bar
{
	-fx-background-color: transparent;
	-fx-font-family: Baumans;
	-fx-font-size: 14px;
	-fx-text-fill: #00CCFF;
}
But guess what? It didn't (obviously) work. Since Menu and MenuItem don't have a setFont() method I guess I'm stuck with this. Thanks in advance
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 11 2012
Added on Mar 13 2012
6 comments
7,735 views