• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why choose JTextArea

 
Greenhorn
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic