File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JCheckboxMenuItem won't change selection color Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JCheckboxMenuItem won Watch "JCheckboxMenuItem won New topic
Author

JCheckboxMenuItem won't change selection color

Stefan Cosma
Greenhorn

Joined: Mar 30, 2012
Posts: 13

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?

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4544
    
    5

You left out one very important piece of information: which Look and Feel?

edit Works for me with the default Metal look and feel.


luck, db
There are no new questions, but there may be new answers.
Stefan Cosma
Greenhorn

Joined: Mar 30, 2012
Posts: 13

Yes, sorry about that! I guess the default one, which I believe is - this - .
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4544
    
    5

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

Joined: Mar 30, 2012
Posts: 13

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

Joined: May 03, 2008
Posts: 4544
    
    5

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

Joined: Mar 30, 2012
Posts: 13

If you go on the first JCheckboxMenuItem, the selectionBackground should be the same color as the second one.


Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4544
    
    5

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

Joined: Mar 30, 2012
Posts: 13

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

Joined: Mar 30, 2012
Posts: 13

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

Joined: May 03, 2008
Posts: 4544
    
    5

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

Joined: Mar 30, 2012
Posts: 13

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

Joined: May 03, 2008
Posts: 4544
    
    5

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JCheckboxMenuItem won't change selection color