3

Now, I know, different font families can be achieved within a JButton text. Like below

JButton button = new JButton("<html><font face=Arial>Hello </font><font face=Verdana>World</font></html>");

It looks like this. "Hello" with Arial and "World" with Verdana.

enter image description here

But, what if I want a word to have a font which I've created using Font.createFont() method. I thought, something like this, would have worked.

Font myFont = createMyFont();
JButton button = new JButton("<html><font face=MyFont>Hello </font>World</html>");

The significance of this question is that I am creating a multilingual software, which has two fonts in a single JButton.

So, I want my JButton to be like this:

enter image description here

But, it is like this:

enter image description here

asked Jan 10, 2014 at 16:55
7
  • The javadoc mentions to use Font.registerFont() after creating the font using Font.createFont() Commented Jan 10, 2014 at 17:16
  • Try register the font: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); ge.registerFont(myFont); Commented Jan 10, 2014 at 17:19
  • And then, from what name should I call it? With its file name? Commented Jan 10, 2014 at 17:22
  • @Akshat Should be the filename Commented Jan 10, 2014 at 17:23
  • Yes, it's working. But, I had to remove the '_' from its font file name "Devlys_010" and code it like <font face=\"Devlys 010\">ABC</font>. Commented Jan 10, 2014 at 17:37

1 Answer 1

2

Register custom font with:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment()
ge.registerFont(myFont);

After, provide file name, something like:

 URL fontUrl;
 try {
 fontUrl = new URL("http://www.webpagepublicity.com/" +
 "free-fonts/a/Airacobra%20Condensed.ttf"); // dummy font 
 Font myFont = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
 myFont = myFont.deriveFont(Font.PLAIN,20);
 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
 ge.registerFont(myFont);
 button.setText("<html><font face='Airacobra Condensed'>Hello </font>World</html>");
 } catch (MalformedURLException e1) {
 // TODO Auto-generated catch block
 e1.printStackTrace();
 } catch (FontFormatException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }

The filename can be accquired on windows by rightclicking on the font file, selecting Properties -> Details and there it's the title. Example: FontAwesome Regular.

enter image description here

Zackline
8341 gold badge9 silver badges29 bronze badges
answered Jan 10, 2014 at 17:43
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.