Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Text Wrapping in JTable - Help!  RSS feed

 
Skip Cole
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have gone nuts trying to get text to wrap in a JTable.
I have tried everything I could think of. I even tried chopping up the text into words, putting those words into Strings, making JLabels with those Strings, and then adding those into a JPanel (with FlowLayout()), and then trying to add that to the cell in the JTable.
Arghh!!!
Skip
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skip,
It's difficult for me to give you any advice that I'm really confident in without knowing more about exactly what you're trying to accomplish. However, based on what little I do know, you probably need to look at creating a JTextArea-based cell renderer.
------------------
Brett Spell
Author, Professional Java Programming
 
Skip Cole
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for your reply.
That is exactly what I have done, and i almost have it working now, but every cell gets the same contents.
Skip
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that you're not calling the JTextArea's setText() method from within your renderer. When the renderer's getTableCellRendererComponent() method is called, you need to update the state of the renderer component (i.e., the JTextArea) using the value of the cell that's about to be rendered. In this case, you need to store the cell's String value in the JTextArea using setText().
One of the parameters passed to getTableCellRendererComponent() is an Object representing the value of the cell about to be rendered, and you should pass a String representation of that value (which you can obtain by calling its toString() method) to the JTextArea's setText() method. I've included an example of how this might appear below:

------------------
Brett Spell
Author, Professional Java Programming
 
Skip Cole
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The cellRenderer is putting the same value in every row. Now I am getting some very interesting results, but I don't understand them.
1.) The table appears to have the same value for every row, but
2.) when I double click on the value, the real value shows up in a large window.
Weird.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!