This week's giveaway is in the Beginning Java forum.
We're giving away four copies of Bad Programming Practices 101 (e-book) and have Karl Beecher on-line!
See this thread for details.
Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

color in cell  RSS feed

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to create your own implementation of TableCellRenderer. Consider extending DefaultTableCellRenderer.
Does that help ?
D.
 
ben riches
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ March 01, 2004: Message edited by: ben riches ]
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
table.setDefaultRenderer( Object.class, new MyTableCellRenderer() );
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!