• 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 Background Color in Rom Camick's ButtonColumn Class to Show Enabled/DIsabled

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to implement ButtonColumn and have most of it figured out. My question is regarding background color for enabling/disabling a button. I know how to enable/disable a button but I also want to change the color to "dim" it so it is obvious it is disabled. In my table model I overrride 'isCellEditable' to enable/disable but I don't know where/how to set the background color at the same time. Can someone suggest how to do that? TIA.
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but I don't know where/how to set the background color at the same time.



You don't set the color at the same time.

You need to change the logic in the renderer. The rendering code looks at the current state of the cell and then renders the button appropriately.

Currently the logic just checks to see if the cell is selected.

You will need to modify the logic to additionally check if the cell is editable. Then you should be able to enable/disable the button as required.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for a good question, Sam. And welcome to the Ranch.
 
Sam Ritter
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:
You will need to modify the logic to additionally check if the cell is editable. Then you should be able to enable/disable the button as required.


Thanks. I am getting back to this now and here is how I understand what you are saying. I need to modify 'getTableCellRendererComponent' and check if the current cell is editable or not. In terms of available methods do you mean I should check "renderButton.isEnabled()" or is there some other component I should check for "isEditable()"?
 
Sam Ritter
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Ritter wrote:
Thanks. I am getting back to this now and here is how I understand what you are saying. I need to modify 'getTableCellRendererComponent' and check if the current cell is editable or not. In terms of available methods do you mean I should check "renderButton.isEnabled()" or is there some other component I should check for "isEditable()"?


I think I figured it out. I need to use "table.isCellEditable(row,column)", right?
 
Sam Ritter
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have it working now. Thanks. As an aside question, what is the default color for a disabled JButton. Apparently it is not 'Color.light_gray'.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

do you mean I should check "renderButton.isEnabled()"



No. A renderer is shared by all cells in the column so the render button state is always reset by the table before rendering of the cell is done so all cells can be rendered the same. You are then adding custom logic for a specific cell.

I think I figured it out. I need to use "table.isCellEditable(row,column)", right?  



Correct.

what is the default color for a disabled JButton



I don't think, you need to know. If you just make the button disabled, the button should paint itself in the disabled state. There should be no need to play with the foreground/background

However, in general you can use the UIManager to determine some of the components defaults. Check out: UIManager Defaults.

 
Sam Ritter
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't seem to change when disabled. I looked at UIManager defaults and "Button.background" is listed which is the enabled color but I don't see anything for disabled.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It doesn't seem to change when disabled.



Which is what should happen. The LAF will paint the button in its disabled state. You should not need to play with any of the button properties other than to set the button disabled.

I don't see anything for disabled



Which tells you there is no configurable property for the disabled background color. There is no need for you to manually set the background, since the UI will do it for you. If you don't want the default disabled background then you set the background to whatever you want.
 
Sam Ritter
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I am now able to disable the JButton and have it change color correctly. Now comes the hard part. When that happens I need to change ALL the buttons in that column of the table. If one button is clicked, only that row is rendered. How can I get the buttons in that column for all the rows to change?

P.S. Let me know if this should be a new thread.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. You need to update the state of your TableModel so that isCellEditable(...) returns false for all the rows of that column.

2. Then you can invoke repaint() on the table and the new state of each cell will be painted.

 
Sam Ritter
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its working. Thanks.
 
I can't take it! You are too smart for me! Here is the tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic