Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

change font in JPanel  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).



 
Marshal
Posts: 59681
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 59681
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding text fields when the text changes? That would produce a very difficult GUI to use.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!