• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why choose JTextArea

 
Mark A Carter
Greenhorn
Posts: 8
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I started making a simple chat program and as it evolved, I wanted to start giving user's the ability to change fonts. At first I built the main screen using JTextArea. This turned out to be a huge problem. When I started to do research, I found it's incredibly difficult to display multiple fonts and font colors on a JTextArea. In then end, I had to switch to a JTextPane.

I know I should've done research before, but I just naively thought all text fields would have the ability to display different colors.

Anyway, as I document my learning experience, I wanted to see if there was any reason to choose JTextArea. I'm assuming, it's more lightweight than JTextPane, but does it really save that much to make a difference. I feel like I should personally act as if JTextArea is deprecated.
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There are several types of text component, some with multiple lines of text, some with one line, some with the ability to implement HTML tags, and I can't remember which is which. But you can read about them in the Java™ Tutorials.

We usually discuss this sort of thing in our GUIs forum, so I shall move you thither.
 
Paul Clapham
Sheriff
Posts: 21865
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good strategy for finding the answer to this question might start with looking in the API documentation, where it says

A JTextArea is a multi-line area that displays plain text.


That should tell you right away that fonts and colours aren't going to be supported. The other good thing about that documentation page is that it goes on by directing you to a tutorial with examples of text components; after you get done going through that tutorial you should have a pretty good idea of which component you need to use.

If the API documentation is something you haven't heard of before, here's a link to the relevant page: JTextArea. You should really have a bookmark in your browser to the API documentation site because it provides information that you need. Also if you find a link to a tutorial there, you should go and read that tutorial if you plan to make significant use of the class. Or alternatively do a web search for java X tutorial, where X is the class or concept you want to learn about.

And, welcome to the Ranch!

 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark A Carter wrote: I wanted to start giving user's the ability to change fonts. At first I built the main screen using JTextArea. This turned out to be a huge problem.


Showing all text in the same font and style is a defining characteristic of JTextArea. However I don't think this could be called a "huge problem."

So you need to be able to mix fonts now? Ok, simply change JTextArea to JTextPane in your code. There may be a few API issues you need to clean up, but probably not too many. Fix those, then you can work on adding font-mixing code.
 
Mark A Carter
Greenhorn
Posts: 8
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I ended up changing it all, but it did take some time. I had to add StyledDocument, wrap every change into try-catch blocks, along with changing the method calls. It was just a hassle even with my small amount of code. I was just picturing what would happen if this was a large application.
 
Paul Clapham
Sheriff
Posts: 21865
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you observed, maintaining styled text is a lot more complicated than maintaining plain text. So your code for JTextPane is naturally going to be larger and more complicated, too.

However if you were writing a large application you would surely modularize your code so that the complicated style-wrangling parts were encapsulated in methods, rather than having complicated style-wrangling code all over the place. I should also point out that you wouldn't get this right the first time (nobody does) and you'd find yourself refactoring the code once you noticed your code was a mess. That's just how it is with writing large applications.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!