• 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

JCheckboxMenuItem won't change selection color

 
Greenhorn
Posts: 13
Opera Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guy,

Am I doing something wrong or what? I want to change the color for the selection background of the JCheckboxMenuItem and it doesn't change at all. This is what I have, what am i doing wrong?

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You left out one very important piece of information: which Look and Feel?

edit Works for me with the default Metal look and feel.
 
Stefan Cosma
Greenhorn
Posts: 13
Opera Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, sorry about that! I guess the default one, which I believe is - this - .
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's the same one. To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. Note: Short. Not all your code.
 
Stefan Cosma
Greenhorn
Posts: 13
Opera Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, well I figured out the actual problem, the UIManager can't apply the selectionBackground color on a static component. I made a simple window with just a menu and a checkbox inside it and what do you know it worked perfectly. So the problem now, is how to make the UIManager work on a static component.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Cosma wrote:the UIManager can't apply the selectionBackground color on a static component.


That's gibberish. Where's that SSCCE?
 
Stefan Cosma
Greenhorn
Posts: 13
Opera Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you go on the first JCheckboxMenuItem, the selectionBackground should be the same color as the second one.


 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's expected. Changing LaF properties doesn't automagically affect components that have already been constructed. Make this change and report back:

edit And please go through the SSCCE link. Your posted code is mostly irrelevant to the stated problem.
 
Stefan Cosma
Greenhorn
Posts: 13
Opera Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It worked perfectly on this example now I just have to try it on the main project. Thank you very much.

Edit: It worked on the main project as well. Thank you again for your help.
 
Stefan Cosma
Greenhorn
Posts: 13
Opera Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have another question. How can I change the selected icon for the CheckBoxMenuItem?

I tried this but it shows only the normal state. When I click, it doesn't change to the checked state.

 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inventing stuff rarely works. I can't see what led you to believe that "CheckBoxMenuItem[Selected].checkIcon" would be recognized by JCheckBoxMenuItem as a relevant UIDefaults key.

You could read the source of the private static class javax.swing.plaf.metal.MetalIconFactory.CheckBoxMenuItemIcon to see how the Swing designers approached the issue.

As I see it, you have two primary options, with several variations possible:
1. Set the selected icon for each item in code, or create and use a custom subclass that does that in its constructor. You may need to experiment to determine which Icon of selected/pressed/rollover is used for the state in which you are interested.
2. Write an Icon implementation that wraps the two icons and decides which to paint based on the state of the component, queried after appropriate casting.

For your future reference, one way you can identify valid UIManager / UIDefaults keys is by using Rob Camick's UIManager Defaults.
 
Stefan Cosma
Greenhorn
Posts: 13
Opera Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I solved the issue. It was actually real easy. I replaced JCheckboxMenuItem with JToggleButton and set the Icon and the selected Icon on each of the elements, and it worked.

This is where I found the solution.

Thank you Darryl for all your help so far.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Cosma wrote:I ... set the Icon and the selected Icon on each of the elements


That was my suggestion at No. 1, two posts up.
 
Good night. Drive safely. Here's a tiny ad for the road:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic