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

HTML in java

omar bili
Ranch Hand

Joined: Aug 13, 2004
Posts: 177
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

Joined: Jul 06, 2004
Posts: 51
Yes you are right.
Ex.
Label.setText("<html><b>Text"); will set the test in bold.
Mahesh x Bogadi
Ranch Hand

Joined: Jul 06, 2004
Posts: 51
For Fonts I don't thing HTML works. You have to use setFont().
Mahesh x Bogadi
Ranch Hand

Joined: Jul 06, 2004
Posts: 51
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

Joined: Aug 13, 2004
Posts: 177
i want to change the font in the JTextArea or JPanel, there is not labels.
Thanks
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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

Joined: Aug 13, 2004
Posts: 177
thanks Layne for the help
Omar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HTML in java