aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes color in cell Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "color in cell" Watch "color in cell" New topic
Author

color in cell

ben riches
Ranch Hand

Joined: Nov 08, 2002
Posts: 126
Hello All,
I want to know how I can change the color of my cells when they gain focus. As at the moment when I click into one of my cells the border of that cell is yellow, and when I start typing in that cell the border then changes to black. I want it so that when I click into that cell it is black aswell.
How can I do this?
Thanks
Ben
Here is my code for my JTable

And here is my code for the scrollpane:
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
You need to create your own implementation of TableCellRenderer. Consider extending DefaultTableCellRenderer.
Does that help ?
D.
ben riches
Ranch Hand

Joined: Nov 08, 2002
Posts: 126
Originally posted by Don Kiddick:
You need to create your own implementation of TableCellRenderer. Consider extending DefaultTableCellRenderer.
Does that help ?
D.


How and where would I put that in my code?
Thanks
Ben
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
To create the renderer :

Then in you code that creates you JTable :

Check this out for more information.

hope that helps,
D.
ben riches
Ranch Hand

Joined: Nov 08, 2002
Posts: 126
Originally posted by Don Kiddick:
To create the renderer :

Then in you code that creates you JTable :

Check this out for more information.

hope that helps,
D.


Hello Don,
I have had a look at the page you gave me but im still having trouble with it.
Here is some code to make a simple JTable.
Could you show me where I should and how to create the Cell Renderer

Thnaks
Ben
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Ok, lets start with the renderer. You need to create a new class like this :
class MyTableCellRenderer extends DefaultTableCellRenderer
Then you need to override getTableCellRendererComponent in your new class. Have a look at the API documentation for TableCellRenderer and the link I posted before. Have a go at writing the getTableCellRendererComponent method then post your code and we can discuss it.
D.
ben riches
Ranch Hand

Joined: Nov 08, 2002
Posts: 126

[ March 01, 2004: Message edited by: ben riches ]
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Excellent stuff !
All you need to do know is to say,
if( isFocused )
setBorder( black border );
else
setBorder( normal border );
Like this :

D.
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
I've made a few more changes here to improve readability, flexibility and performance.

let me know if it doesn't make sense.
D.
ben riches
Ranch Hand

Joined: Nov 08, 2002
Posts: 126
Hello Don,
Thanks for your help so far.
How do I use that class in my code so that I get that color to work with my JTable?
Thanks
Ben
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
table.setDefaultRenderer( Object.class, new MyTableCellRenderer() );
 
 
subject: color in cell