| Author |
Centering Text in a JTable
|
Jason Nesbitt
Greenhorn
Joined: Aug 16, 2004
Posts: 16
|
|
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
Joined: May 18, 2004
Posts: 434
|
|
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
Joined: Dec 12, 2002
Posts: 580
|
|
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
Joined: May 18, 2004
Posts: 434
|
|
|
So you can use the DefaultTableCellRenderer and then inside that you override the label to center the text?
|
 |
Don Kiddick
Ranch Hand
Joined: Dec 12, 2002
Posts: 580
|
|
You don't necessarily have to do that. DefaultTableCellRenderer *extends* JLabel so you can do : D.
|
 |
Rachel Swailes
Ranch Hand
Joined: May 18, 2004
Posts: 434
|
|
OMG! That's like way easier to do the simple stuff. Thanks Don!
|
 |
Adam Price
Ranch Hand
Joined: Nov 11, 2005
Posts: 95
|
|
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
Joined: Feb 23, 2007
Posts: 1561
|
|
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
Joined: Jan 14, 2004
Posts: 1535
|
|
|
|
 |
Adam Price
Ranch Hand
Joined: Nov 11, 2005
Posts: 95
|
|
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.
|
 |
 |
|
|
subject: Centering Text in a JTable
|
|
|