This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable Cell Renderer  RSS feed

 
pindur pandur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a JTable application using Swing, and the document underlying the
table view may contain numbers and text. I want numbers to appear right aligned and text to appear left aligned. I searched the net, with no luck, probably because it's something simple that everyone knows how to do. The columns are not designated as number only, or text only, it's all mixed. Any suggestions would be appreciated.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to know about the javax.swing.table.TableCellRenderer already. What specific question about its use do you have? The Swing tutorial has a section on using them.
 
pindur pandur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is that if I write a cell Renderer for numbers that right aligns numbers and another one for text, that left aligns text, how will I let the table know which one to use depending on the type of data that needs to be displayed. I'm not sure if this makes sense, but in the examples on the web, it's usually a column that has the same type of data, and the cell renderer gets added to the column...
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's why the getTableCellRendererComponent method gets passed the row and column indices of the cell to render. Using that, it needs to figure out itself which cell to draw in which way.
 
pindur pandur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can use different cell renderers for different columns.
You can set a cell renderer for a specific column like this:

header must be a column identifier - usually the header String you used for the column.
With this column you can do several things - setting minimum and/or maximum sizes for instance.
 
pindur pandur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My problem is that a column contains numbers and text. If it contained just numbers, or just one type of data, it would be easier... So I'm trying to implement the public Component getTableCellRendererComponent method; so far I did not get it to work,everything is still left-aligned.
 
pindur pandur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it to work, but I'm sure this is not the best solution:

public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int col) {
double numberValue;
try{
numberValue = Double.parseDouble((String)value);
setText((String)value);
setHorizontalAlignment(SwingConstants.RIGHT);

}
catch(NumberFormatException e){

setHorizontalAlignment(SwingConstants.LEFT);
setText((String)value);
}
return this;
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Swing/AWT...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!