Dev Ops in 30 Days

Java GUI JTable Text Wrapping

Hi everyone!
Thanks for visiting my blog again!

Today I would like to share about my findings about JTable Text Wrapping!


Like what is shown in this photo! Took from : http://www.java2s.com/

It seems easy and common, but it is not that easy as you think.

~~~~~


JTable. Taken from: http://www.javalobby.org/articles/jtable/?source=archives

If you have no idea what JTable is, you should see this tutorial first. :)

If you Google about JTable Text Wrapping, you would probably find out that you need TableCellRenderer to do it!

According to the description of its API:
This interface defines the method required by any object that would like to be a renderer for cells in a JTable.

So, if you want to format your table cell nicely, probably you need a TableCellRender to do it! However, TableCellRender is just an interface, you would need to implement it or using a DefaultTableCellRenderer to format your cell.

However, the DefaultTableCellRender is implemented using JLabel. Remember what is our original question?
We want to make the cell has the ability to do text wrapping.

It is possible to make a JLabel to have the TextWrapping property, but it is not very neat compared to JTextArea. See here for more information.

Now, we want to have a TextWrapping JTable cell, but DefaultTableCellRender is hard to do that. So, the only way is to implement TableCellRender ourselves using JTextArea.

So, the normal way to do that is just override the getTableCellRendererComponent() function (which uses to customize the table cell) and set the TextArea to have the lineWrapping property. However, it does not work properly as mentioned in this question. The reason is believed to be: JTable's row's height has fixed!

The other things that would come to your mind is to make the row's height flexible.

After Google Searching for a long time, I finally found this solution! You can see the solution in the link below also:
http://manivelcode.blogspot.sg/2008/08/how-to-wrap-text-inside-cells-of-jtable.html

This solution might not be the neatest solution but it works great for me!
The solution uses the DefaultTableCellRenderer to help it to succeed.
The code is pretty self - explanatory, as the author of the solution has explained most of his code, so I would not explain again. I just want to share this solution out, so that the Java GUI Beginners like me can learn about this good solution. (Well, at least for me.) ^^

~~~~~

Hope that you like it!

Enjoy programming!

Jun Wei
5/4/14
7:29pm


Comments

  1. In this is just a very simple short WordPress tutorial videos tutorial on how to wrap text around a video I've also got a video on this blog post how to read wrap text around an image or a picture very very simple to to wrap text around and imaging WordPress of I've made up a video not put that on this blog post as well that the blog post is actually about how to wrap text around a video so if you don't want to just put the video up the top here you want to put it to the size to the left of the right it's very good very easy but what you need to do is you need to add a bit of code so I've written that code and I've put it in pink here on my site on this on this actual blog post only
    To know more click here

    ReplyDelete
    Replies
    1. Thanks for your comment. I will provide more details next time. :D

      Delete

Post a Comment