Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Centering Text in a JTable

 
Jason Nesbitt
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to center the text in some of my columns. So far, the only way I have found to do this is to create a new CellRenderer which extends JLabel and implements TableCellRenderer. I then add this cell renderer to the appropriate column. This sure does seem like a complicated solution to do something simple. Am I missing something?

Thanks
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, you aren't missing anything. I went searching for the same answer and I landed up using the tableCellRenderer classes to accomplish this. It does seem like overkill, but when you do it a couple of times it is actually quite fast to incorporate into the code.

Cheers,
Rachel
[ November 09, 2004: Message edited by: Rachel Swailes ]
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always just use DefaultTableCellRenderer (which allready implements TableCellRenderer and extends JLabel). This will also perform faster - check the implementation notes.
D.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you can use the DefaultTableCellRenderer and then inside that you override the label to center the text?
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't necessarily have to do that. DefaultTableCellRenderer *extends* JLabel so you can do :



D.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OMG! That's like way easier to do the simple stuff.

Thanks Don!
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don Kiddick wrote:You don't necessarily have to do that. DefaultTableCellRenderer *extends* JLabel so you can do :




Sorry to drag up an ages-old post, but could someone help me understand how this advice is implemented? Where do I declare the DefaultTableCellRenderer and how do I actually apply it to cells in the jTable?

Thanks,

Adam
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Price wrote:Sorry to drag up an ages-old post, but could someone help me understand how this advice is implemented? Where do I declare the DefaultTableCellRenderer and how do I actually apply it to cells in the jTable?

Have you gone through the Sun Swing tutorial, specifically the JTable section? It's all pretty much laid out for you there.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig Wood wrote:


That is very helpful - thank you. I am using netbeans* and based on your tip, I added
as pre-creation code on the main panel in which the JTable resides. Then I used the Table Contens ->Columns -> renderer option to set the custom renderer as dtcr. That works for the column data, and I think it points me in the right direction to figure out how to center the column headers as well.

Thanks again!

-Adam


*I know that isn't a good way to learn Java, but I am not trying to learn java - I am trying to write a program that will help me with my graduate school research on chemical adsorption isotherms.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic