This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
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.
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.