• Post Reply Bookmark Topic Watch Topic
  • New Topic

How heavy is JTextPane  RSS feed

 
Mark A Carter
Greenhorn
Posts: 8
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried doing research on JTextPane vs JTextArea and JTextPane has a lot to offer. However, JTextArea is suppose to be more lightweight. Does anyone know how much memory is being saved by choosing JTextArea over JTextPane, or is there a way I can find out?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be a way you can find out, but it would probably be a waste of time. Remember that you have at least a gigabyte of memory to play with, so worrying about the memory usage of one single object isn't useful. Now if you planned to have 100,000 JTextPane objects then the memory consumed by each of them could be a serious issue -- but you don't plan to do that, do you?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. No.

I have already directed you to the Java™ Tutorials about text components. Does it say anything about heavyweight and lightweight? Who said a text area is lighter?
You choose components based on the functionality you require not on memory use. The size differences in the Class<T> file are likely to be much larger than the size differences in the objects. The memory use of the objects is likely to be proportional to the number of fields, but that is only a rough guide. Any differences are hidden by the implementation and may vary from one implementation to another and one version to another. I don't know any way to find out such memory use, nor do I think is it going to be a worthwhile exercise if you can buy additional memory for less than the cost in time and effort to calculate memory use.
 
Mark A Carter
Greenhorn
Posts: 8
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that JTextPane can do everything that JTextArea can, but more. Would it be best practice just to always choose JTextPane, in case you ever want to add more features int future. What would be the negative repercussions of always choosing JTextPane.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark A Carter wrote:Would it be best practice just to always choose JTextPane, in case you ever want to add more features int future.


No, not really. There's a principle in programming called YAGNI, which stands for "You Ain't Gonna Need It". The meaning of the principle is, don't put code into your application on the grounds that you might need it later. Because you ain't gonna need it.

Or if it turns out that you do need it, then put the code in when you find that out. Otherwise you're going to have unused code lying around which has to be maintained.

Now in this example the unused code in question is inside the JTextPane class, i.e. all the code which differentiates it from JTextArea, and you wouldn't have to maintain it. So it would appear that YAGNI isn't particularly relevant here. But there's a related principle which says "Do the simplest thing which could possibly work", and that suggests you should use JTextArea because it's simpler than JTextPane. But again, for you as the maintainer of the code it's only trivially simpler.

So: you've got two best practices which say to use JTextArea instead of JTextPane, although weakly. However both best practices are rejecting the idea of doing things in case you need them later, so I'd say the answer to your question is NO.
 
Mark A Carter
Greenhorn
Posts: 8
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTextArea was definitely simpler, thanks. I actually just heard about YAGNI briefly in a talk, never even thought about it in this case.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!