File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Formatting JTable columns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Formatting JTable columns" Watch "Formatting JTable columns" New topic

Formatting JTable columns

Douglas Kent
Ranch Hand

Joined: Oct 06, 2000
Posts: 171
Can't find how to center-justify or right-justify data being displayed in my JTable. Also, how would one add dollar signs?
Matt Robinson

Joined: Apr 30, 2001
Posts: 28
Your cell renderer is, by default, an extension of JLabel. Change the horizontal alignment of the renderer you use for each column you want different alignment for.
You can also add a $ dollar sign in front of any data before it is painted by the cell renderer.
Matthew Robinson co-author of "Swing" (1999, Manning Publications). Freely available at

Matthew Robinson<BR><BR>co-author of "<a href= target=newwindow rel="nofollow">Swing</a>" (1999, Manning Publications). Freely available at <A HREF="" TARGET=_blank rel="nofollow"></A>
Matt Robinson

Joined: Apr 30, 2001
Posts: 28
Just make sure not to add the $ to the real data stored in your table model... only use it for rendering purposes
Matthew Robinson co-author of "Swing" (1999, Manning Publications). Freely available at
Jay S

Joined: Jan 09, 2001
Posts: 13
Hello Matt,
I had the same problem. Actually I am using JClass software for sortable tables. I am sending in a DefaultTablemodel into a JCSortableTable (this extends JTable). I have boolean values(check boxes) in my data. Now when the table is displayed my check box column is left-aligned. I want that to be center-aligned. I tried to the following code but it gave me a class cast exception.
JCSortableTable table = new JCSortableTable(DefaultTableModel TM)
Class ck = table.getColumnClass(4);
doesnt work this way b'cos getDefaultRenderer(..) returns a TableCellRenderer interface and not DefaultTableCellRenderer object...
how do i do that
thanks for ur help..
Brian Snyder2002

Joined: Jan 24, 2002
Posts: 11
Hi Jay,
I had the same problem. In your rendering class, you should create a panel and add to it whatever you like. Then return ther panel as your returning component. Let me know how things go.

Java Developer<br />SCJ2P
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Jay S & Brian Snyder2002,

Please re-register with usernames that follow the JavaRanch UserName Guidelines. Your usernames have to be in the form "First Name" + space + "Last Name", where first and last names are longer than one character, and where user names are not obviously fake.



Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Formatting JTable columns
jQuery in Action, 3rd edition