|
-
Mar 17th, 2004, 01:40 AM
#1
Thread Starter
Banned
A customized JTable...
Is it possible to have different renderers for cells in a JTable... Let's just say one column has different kinds of data..
You want one data to be rendered with a black colored font and the last element in the column be rendered blue?
Can this be done I mean individually setting the cells of a column... Can anyone give me a code or a good site that shows how to customize JTable... I know very little and it only involves data that is same for all cells in a column...
-
Apr 12th, 2004, 04:11 PM
#2
Addicted Member
How custom?
Hello
I think i know what you mean, so here we go
The JTable can take any number of cell renderers, and these are assigned to the column type, e.g. if the column is representing String.class, then you can register a renderer with the Jtable to just render the String column:
Code:
setDefaultRenderer(String.class, new MyStringRenderer)
This means that whenever the table trys to render an object of type String it will use the MyStringRenderer.
You then have to implement MyStringRenderer, now if you choose to implement DefaultTableCellRenderer then you only have to play with one method, and its pretty much a JLabel underneath it all, so you can change the colour of the text and backgrounds etc.. i'm sure you get the picture.
Suns Java site is pretty good for this: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
Now i wasn't sure if you wanted entire columns rendered or individual cells, if you wanted individual cells then you could implement a Renderer and use the Column and Row coordinates to decide what was in it, or use the Object to determine what you were looking at. you'd then simply register it in on the JTable against the Object.class, and i think.. and don't hold me to this, that it will try and render everything for you!
helpful? or have i missed the point?!
Andy
-
Apr 13th, 2004, 07:50 PM
#3
Thread Starter
Banned
Re: How custom?
Originally posted by Andy_Hollywood
Hello
I think i know what you mean, so here we go
The JTable can take any number of cell renderers, and these are assigned to the column type, e.g. if the column is representing String.class, then you can register a renderer with the Jtable to just render the String column:
Code:
setDefaultRenderer(String.class, new MyStringRenderer)
This means that whenever the table trys to render an object of type String it will use the MyStringRenderer.
You then have to implement MyStringRenderer, now if you choose to implement DefaultTableCellRenderer then you only have to play with one method, and its pretty much a JLabel underneath it all, so you can change the colour of the text and backgrounds etc.. i'm sure you get the picture.
Suns Java site is pretty good for this: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
Now i wasn't sure if you wanted entire columns rendered or individual cells, if you wanted individual cells then you could implement a Renderer and use the Column and Row coordinates to decide what was in it, or use the Object to determine what you were looking at. you'd then simply register it in on the JTable against the Object.class, and i think.. and don't hold me to this, that it will try and render everything for you!
helpful? or have i missed the point?!
Andy
Helpful.... .
-
Apr 14th, 2004, 12:47 PM
#4
Addicted Member
serious?! i thought i just skilfully skirted the problem!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|