Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HTML in java

 
omar bili
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
i want to allow the user to change the font of the text in the (JTextArea or ?) just like word and allow him to make a part of the text in bold.
do i have to use html format in a java component?
please help
Thanks
bili
 
Mahesh x Bogadi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are right.
Ex.
Label.setText("<html><b>Text"); will set the test in bold.
 
Mahesh x Bogadi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Fonts I don't thing HTML works. You have to use setFont().
 
Mahesh x Bogadi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well
<h1> and <h3> etc tags work.

add(new JLabel("<html><p><font size=2 face=Verdana>This is a paragraph.</font></p><p><font size=3 face=Times>This is another paragraph.</font></p>"), BorderLayout.CENTER);

this also works.
Hence font can be set using HTML tags.
 
omar bili
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to change the font in the JTextArea or JPanel, there is not labels.
Thanks
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am willing to bet that you can do this, but I don't know how off the top of my head. Let me give you a little explanation of what I DO know and how I would go about figuring out the rest. I hope this gives you some information to get started.

First of all, I suggest you bookmark The Java Tutorial and the Java API docs. These are both great resources when trying to figure out how to do something in Java. The first contains "trails" for specific parts of the Java API and provides an overview for many of the classes it contains. The second provides detailed descriptions of each class and method in the API. It is organized by package and class, so it's difficult to use as a tutorial. However, if you need to look up the details of a specific class or method, it gives all the information you need (and more)!

Next, it will be helpful if you understand the Model-View-Controller design pattern that Swing uses extensively. In this case, the JTextArea is the Model which contains only the logic of how the control is displayed on the screen. The actual text and formatting is stored in the Model. From my brief look at the API documentation, it looks like the Document class provides the model for all classes that extend JTextComponent (including JTextArea). JTextComponent contains a getDocument() method that returns the underlying Document object. I am willing to bet that this will provide some way to manipulate the font of the text in the JTextArea.

As I said earlier, I don't know all the details, but this is where I would start exploring to figure out how to do it. I hope this helps get you started. Please come back with more questions as you continue working on this. We will be glad to help.

Regards,

Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p.s. Check outthis section from the Java Tutorial. It looks like it shows how to do what you want. If you want all the text in the same font, it looks like JTextArea will work fine. However, if you want something more like Word where each character can have it's own font, it looks like you need to use JTextPane instead.

HTH
 
omar bili
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Layne for the help
Omar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic