File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formatting JTable columns

 
Douglas Kent
Ranch Hand
Posts: 171
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matt Robinson
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic