Java Text Formatting bold
Asked Answered
E

2

5

I am having a hard time understanding how to bold the text in my GUI program. The program shows the initial value of my calculator program to be 0.0 but I need to be able to make it bold and set it to 14 font. Is there any easy way to do this?

JPanel x = new JPanel(new BorderLayout());
         JTextField z = new JTextField();
         z.setEditable(false);
         z.setText("0.0");
         x.add(field, BorderLayout.NORTH);
Evvy answered 3/4, 2014 at 19:28 Comment(0)
T
18

Try this:

z.setFont(z.getFont().deriveFont(Font.BOLD, 14f));

deriveFont() has the advantage of being able to base your new font on the existing one. This will maintain the font characteristics that you don't mean to change.

Tehee answered 3/4, 2014 at 19:30 Comment(1)
That's exactly what I needed! Thank you! I kept forgetting the deriveFont and was putting a comma instead of a period.Evvy
K
0

You can simply change you JTextField font by doing the following:

f.setFont(new Font("Tahoma", Font.BOLD, 14));// Tahoma is an example, you could use any forn you want.
Kellsie answered 3/4, 2014 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.