• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable Cell Renderer

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Swing/AWT...
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic