Results 1 to 5 of 5
Thread: Right align a cell in JTable
- 08-02-2012, 11:06 AM #1
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
- 08-02-2012, 02:05 PM #2
Re: Right align a cell in JTable
Moved from New to Java.
Read the API for JTable and follow the link to the tutorial. Go through the section related to renderers.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-03-2012, 08:32 AM #3
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Re: Right align a cell in JTable
Good tip. I solved this almost perfect with this code:
However the HeaderRenderer made the background color on this cellheader disappeared. Any suggestions on this?Java Code:ledgerTable.setModel(new Classes.LedgerGrid()); TableColumn col = ledgerTable.getColumn("Saldo"); DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer(); dtcr.setHorizontalAlignment(SwingConstants.RIGHT); col.setCellRenderer(dtcr); col.setHeaderRenderer(dtcr);
- 08-03-2012, 09:16 AM #4
Re: Right align a cell in JTable
You could try my Default Table Header Cell Renderer « Java Tips Weblog for the header. Be sure to go through and understand the code.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-03-2012, 03:18 PM #5
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Re: Right align a cell in JTable
Thanks for the tip Darryl, but I only get all the headers aligned. Any suggestions how to align only one of the cells?
This right align the content of my table column named "Saldo" and all the headers. In the DefaultTableHeaderCellRenderer class i have this:Java Code:ledgerTable.setModel(new Classes.LedgerGrid()); TableColumn col = ledgerTable.getColumn("Saldo"); DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer(); dtcr.setHorizontalAlignment(SwingConstants.RIGHT); col.setCellRenderer(dtcr); ledgerTable.getTableHeader().setDefaultRenderer(new DefaultTableHeaderCellRenderer());
But I only wants it for the "Saldo" column.Java Code:setHorizontalAlignment(RIGHT);
I managed this, but not completely as I would like. The header design is changed on this column.
I altered the code like this:
Java Code:DefaultTableHeaderCellRenderer dth = new DefaultTableHeaderCellRenderer(); col.setHeaderRenderer(dth);

As you can see the design is not like I want it to be on this column...
EDIT: Added code
EDIT2: Adjusted codeLast edited by asai; 08-03-2012 at 04:20 PM.
Similar Threads
-
How to horizontal align multi-line Text in a JTable-Cell
By Hotkey in forum AWT / SwingReplies: 3Last Post: 01-10-2012, 10:33 AM -
Align Row to Right in JTable
By shomid in forum AWT / SwingReplies: 2Last Post: 11-25-2011, 10:23 PM -
Render Text Format & Align Cell Border while Exporting PPT Files
By sherazam in forum Java SoftwareReplies: 0Last Post: 01-31-2011, 09:08 AM -
Setting jTable cell?
By greatmajestics in forum New To JavaReplies: 5Last Post: 04-22-2010, 05:33 PM -
Right Align columns in JTable
By Laura Warren in forum New To JavaReplies: 2Last Post: 12-18-2008, 09:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks