aspose file tools*
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
Author

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
Greenhorn

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 http://www.spindoczine.com/sbe


Matthew Robinson<BR>matt@mattrobinson.com<BR>co-author of "<a href=http://www.amazon.com/exec/obidos/ASIN/189311578X/ref=ase_electricporkchop/107-7882751-0234939 target=newwindow rel="nofollow">Swing</a>" (1999, Manning Publications). Freely available at <A HREF="http://www.spindoczine.com/sbe" TARGET=_blank rel="nofollow">http://www.spindoczine.com/sbe</A>
Matt Robinson
Greenhorn

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 http://www.spindoczine.com/sbe
Jay S
Greenhorn

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);
table.getDefaultRenderer(ck).setHorizontalAlignment(SwingConstants.CENTER);
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
Greenhorn

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.
Brian
SCJ2P


Java Developer<br />SCJ2P
Nathan Pruett
Bartender

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.

Thanks,

-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Formatting JTable columns