aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes change font in JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "change font in JPanel" Watch "change font in JPanel" New topic
Author

change font in JPanel

Timo Lumme
Greenhorn

Joined: Jan 05, 2007
Posts: 20
hi,
i havent been coding for some time ..
so how i set string1 = to use n font and string2 to use other font
i want use Verdana", Font.PLAIN, 24 in LottoGuiText1 and after change in to Crystal font ", Font.PLAIN, 24 to TestText string (numbers)
(ps i have downloaded the crystal font from internet, you might not see it).



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You can't apply a Font to a String, only to a Component (or similar).
You might be able to add a Listener to your text components which tells you when they change, and whether you have used "1 2 3 4 5 6 7 8" or not, then change the Font. But that sort of thing is usually discussed on the Swing forum, so I shall move you there.

By the way: Do you really want those String copy constructors?
Timo Lumme
Greenhorn

Joined: Jan 05, 2007
Posts: 20
hmm..just thinking if i add a textfield or such , or add a new label after that, for the numbers then use there a other font would
that do the trick?
i thought it was it possible to apply a Font to a String.. or other way around..

well thanks anyway ,
oh well, back to drawing board..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Adding text fields when the text changes? That would produce a very difficult GUI to use.
Chris Nash
Greenhorn

Joined: May 20, 2009
Posts: 28
I'm having a little trouble understanding what it is you're trying to accomplish. Do you want to display one JLabel with one Font and another with a different Font? That's easy, JLabel has a setFont method. All you have to do is send it the appropriate Font (it looks like font2 is the Font you want from your example). If you're trying to do something else, please provide more information.
Timo Lumme
Greenhorn

Joined: Jan 05, 2007
Posts: 20
yes the first text is static doesnt change and numbers get´s changed (updated)
but change the numbers to font2 in one jpanel? i have add a new jpanel..and set there to use font2.
is it possible to change a font style in middle of jpanel? text1 = verdana + text2=font2


"Do you want to display one JLabel with one Font and another with a different Font? That's easy, JLabel has a setFont method."
correct !
how do that here :
label1 = new JLabel(LottoGuiText1 setFont(font2) + TestText); ???

Chris Nash
Greenhorn

Joined: May 20, 2009
Posts: 28
"Do you want to display one JLabel with one Font and another with a different Font? That's easy, JLabel has a setFont method."
correct !
how do that here :
label1 = new JLabel(LottoGuiText1 setFont(font2) + TestText); ???


After you do that, do this:

That will assign one single font to your JLabel.

is it possible to change a font style in middle of jpanel? text1 = verdana + text2=font2

I see now. No, not if you play by the rules. But there is a way to do it. You have to override the paintComponent() method and do all the drawing yourself. You'll probably have to measure all the font metrics for the strings you render so after you draw one string with one font, you know where to position the next string with the new font. I'm doing precisely this in several of my applications. You'll want to look at FontRenderContext and TextLayout. You'll use them within paintComponent() when you do your drawing.

Hope this helps. Ask if you have any further questions.
Timo Lumme
Greenhorn

Joined: Jan 05, 2007
Posts: 20
hi,
well i ended up setting the layout with buttons and that worked fine
no need to hassle with many jpanel´s etc.

http://img188.imageshack.us/img188/186/lotto.jpg



bye
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: change font in JPanel