• 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

Text alignment in a JTable

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
is it easy to sort out the alignment of integers in my JTable ? Currently they are all over the place, i'd like them aligned to the right.
Also, can I (easily) format any negative numbers so they are i red, with brackets round them ?
Many thanks,
kate
 
kate damond
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, now I've figured out that the default cell renderer would automatically align the numbers in a number column to the right. So, the problem is that my data arrives as an array of Strings.
I think I need to use a new cell renderer for the columns that contain "numbers" (I know which ones they are). So, I have created a new DefaultCellRenderer and set theHorizontal alignment to RIGHT. This all works fine.
In order to complete the other half of the task, though, which is to colour negative numbers in red, I have to find something that will allow me to format individual cells. So, although I can quite happily create a class for my own sort of cell renderer, this won't help will it ?
Any help would be appreciated
Kate
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should work fine the DefaultTableCellRenderer class has a method
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
Use the column to find your number column and the value to check for your negative number. Convert the string to int. Just override this method to do what you want.
 
kate damond
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,
thanks for your help - I'm not sure I have understood it fully ! I have my own cell renderer class, called NumberCellRenderer. Ok, so I can override the method getTableCellRendererComponent. I'm with you to there, then i get confused. This method should return a 'component'. I can send it (in amongst lots of other parameters) the row and column in question, and the value at that cell ? Thsn I can set the colour of the cell presumably, if hte value is negative. But I have to return something. What should I return ?
I have a vague feeling that I'm missing the plot a bit here.
Thanks,
Kate
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just need the statement:
return this;
Since DefaultTableCellRenderer extends JLabel. You are returning a JLabel with the above statement. So any code for color etc. treat it just like a JLabel.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kate
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)

You can check whether the Object(value) is an instance of an integer, if so you can do the process of elimination of negative sign and set the value with the method
setText(your value);
if it not an instance of integer then
default : setText(value.toString());

 
kate damond
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello, me again...
ok, let's forget about testing the String for now - I'll be happy if I can just get it to draw ALL the entries in a column in red for now !!! OK, I have this NumberCellRenderer, which extends DefaultTableCellRenderer. I've had this class around for a while : in its constructor, I set its horizontalAlignment to RIGHT because I want this to apply to all cells, no testing required. (and that's the bit that works)
So, now I have overridden the getTableCellRendererComponent method of NumberCellRenderer, ...

which not only doesn't draw anything in red, but it also doesn't output "Oi !" either.
So, i guess I need to know what this method is supposed to do, and I need to call it ? I mean, it's not even being called. Who should call it and when ?
Many thanks
Kate
p.s. I have read the documentation too, and I'm afraid I understood very little of it !!!
 
Rajan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kate
I have overridden the default cell renderer with mine, in which i have set both foreground as well as background color.
you can set the color in either in your NumberCellRenderer constructor or in getTableCellRendererComponent of NumberCellRenderer.
Code:
setBackground(Color.white);
setForeground(Color.red);

I suppose, is this your renderer declaration
class NumberCellRenderer extends JLabel implements TableCellRenderer{}
Overriding of DefaultCellRenderer is customize you cells in the table. Method getTableCellRendererComponent is used when you set the column with your NumberCellRenderer.

 
kate damond
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, thnaks, I'm nearly there, I think I see what is supposed to happen - certainly, if I set the Foreground / background / font, whatever, in my NumberCellRenderer() constructor, this gets picked up no problem. But, doing the same thing in the getTableCellRendererComponent(...) method of NumberCellRenderer has no effect, because (apparently) this method is never used.
I need to know what you mean by "when you set the column with your NumberCellRenderer." in the above answer. As far as I can see, I don't set any columns with my NumberCellRenderer.
Thanks agan for your input .. honest, I'm nearly there ...
Kate

 
Rajan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kate,
I would like to know how do you associate your NumberCellRenderer to the cells of the taable.
inorder to associate the renderer, i have done it by obtaining the columnModel of the table, with that get the column from the model by using index and set the renderer to the column(which the renderer is set to all the cells of the column).
code:
table.getColumnModel().getColumn(index).setCellRenderer(numbercellrenderer).

if you have done in different way let me know. how u have associated your renderer to the cell.

thanks......
 
kate damond
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK ! Finally completed (seemingly trivial) task - ok, it took me ages, but with all your respective help, tips and pointers I have got the Tables looking good - and learnt a lot.
Now, only one last thing ... my numbers now look like 1234 in black if positive, (4312) in red if negative ,.... can I make them 1,234 and (4,321) ??? ie is there a number formatter type of thingy ? I've been reading about this jaa.text.Format, but it keeps confusing me talking about locales. Looks like overkill to me, I just want to put some commas in !
Cheers,
Kate
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But you learned more by getting it in bits and pieces right? Take a look at DecimalFormat it will do what you want. applyPattern()

Here is a site with swing examples
Swing Examples
There are some good swing examples at this site. Tables in particular.

[This message has been edited by Paul Stevens (edited June 20, 2001).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic