?
Solved

Changing checkbox appearance

Posted on 1998-08-28
15
Medium Priority
?
395 Views
Last Modified: 2010-03-30
I am trying to create a tree (JTree) where each node is a checkbox (JCheckBox class) which can be in one of three states: unselected, selected along with all the children and selected along with some children. I am trying to change the color of the box to depict the third state (the box is light gray instead of white and checked) but unable to do so.

Any suggestions?
0
Comment
Question by:msmolyak
  • 7
  • 4
  • 4
15 Comments
 
LVL 5

Author Comment

by:msmolyak
ID: 1222843
Edited text of question
0
 
LVL 3

Expert Comment

by:sailwind
ID: 1222844
Even experts need to ask some question sometimes eh? Hehehe.

In your tree cell renderer, check the state of the current item.
If the tree leaf is expanded, then you should set the color to
the third state.

I believe the getTreeCellRendererComponent have the following arguments:
JTree tree, Object value, boolean selected, boolean expanded, boolean leaf,
int row, boolean hasFocus.

So just check to see whether it's expanded or not and change the color accordingly

Or a more complicated way is to add a tree expansion listener, and manually
change it when a tree is expanded.





0
 
LVL 5

Author Comment

by:msmolyak
ID: 1222845
My question is actually about physically changing the color of the box. I know when to change it, I don't know how.

Sorry for the confusion.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:sailwind
ID: 1222846
In the tree renderer, can't you just do:

if(expanded)
      myCheckBox.setBackground(Color.white);
else if(selected)
      myCheckBox.setBackground(Color.lightGray);
else
      myCheckBox.setBackground(Color.white);

return myCheckBox;
     
0
 
LVL 5

Author Comment

by:msmolyak
ID: 1222847
That changes the background color of the entire field, but not the little box itself. I need to change the color of the box but not the rest of the field.
0
 
LVL 3

Expert Comment

by:sailwind
ID: 1222848
Oh... I see what you're saying....

I ran a coupla tests with a checkbox on a JPanel.
Here's what I came up with:

background color of checkbox is the entire selection's color
foreground color is the color of the text.

Sorry, after reviewing the release notes, I don't see how you can just
change the color of the box itself. Unless you want to write a custom
checkbox of your own....
0
 
LVL 5

Author Comment

by:msmolyak
ID: 1222849
No, I'd rather not rewrite a checkbox. Thanks for trying.
0
 
LVL 5

Author Comment

by:msmolyak
ID: 1222850
BTW, when you click on checkbox the color of the little box does change (to light gray) while you are holding your mouse down. That is the effect I am trying to achieve.
0
 
LVL 3

Expert Comment

by:sailwind
ID: 1222851
Yeah I see it too. I'm wondering if it's done with their
custom paint look and feel.
0
 
LVL 8

Accepted Solution

by:
diakov earned 300 total points
ID: 1222852
Hi,

You can set the appropriate Icon to the JCheckBox in the        getListCellRendererComponent.

For example:
If (((ModelCell)value).state = 3)
  setIcon(new ImageIcon("c:\\3.gif"));

You might need to adjust some other icons too.

Cheers,
  Nik
0
 
LVL 5

Author Comment

by:msmolyak
ID: 1222853
Spasibo za pomosch', Nikolai.

I ended up stealing the code from WindowsIconFactory class where checkbox icon gets generated. I modified this code slightly to take a new state into account. And in the renderer I do set up new icon as

new FolderCheckBoxIcon()

    private class FolderCheckBoxIcon implements Icon, Serializable
    {
        FolderTreeNode node;

        FolderCheckBoxIcon(FolderTreeNode node)
        {
            this.node = node;
        }

          final int csize = 13;
          public void paintIcon(Component c, Graphics g, int x, int y)
          {
              AbstractButton b = (AbstractButton) c;
              ButtonModel model = b.getModel();

              // outer bevel
              g.setColor(BasicGraphicsUtils.control);
              g.fill3DRect(x, y, csize, csize, false);

              // inner bevel
              g.setColor(BasicGraphicsUtils.controlShadow);
              g.fill3DRect(x+1, y+1, csize-2, csize-2, false);

                // inside box
              if((model.isPressed() && model.isArmed()) || !model.isEnabled() || node.getState() == node.SOME_SELECTED)  // ******This is the line I modified
              {
                    g.setColor(BasicGraphicsUtils.control);
              }
              else
              {
                    g.setColor(BasicGraphicsUtils.controlWhite);
              }
              g.fillRect(x+2, y+2, csize-4, csize-4);

              if(model.isEnabled()) {
                g.setColor(BasicGraphicsUtils.controlBlack);
              } else {
                g.setColor(BasicGraphicsUtils.controlShadow);
              }

              // paint check
              if (model.isSelected()) {
                g.drawLine(x+9, y+3, x+9, y+3);
                g.drawLine(x+8, y+4, x+9, y+4);
                g.drawLine(x+7, y+5, x+9, y+5);
                g.drawLine(x+6, y+6, x+8, y+6);
                g.drawLine(x+3, y+7, x+7, y+7);
                g.drawLine(x+4, y+8, x+6, y+8);
                g.drawLine(x+5, y+9, x+5, y+9);
                g.drawLine(x+3, y+5, x+3, y+5);
                g.drawLine(x+3, y+6, x+4, y+6);
              }
          }

          public int getIconWidth()
          {
              return csize;
          }

          public int getIconHeight()
          {
              return csize;
          }
    }

The problem now is that I lost the tree icon (open/closed folder or leaf symbol). Any idea how can I get those back?
0
 
LVL 8

Expert Comment

by:diakov
ID: 1222854
Hi 'msmolyak',

Forgive my bad Russian, but 'Ne za chem'.

I don't know how to get the tree Icon. Are you sure it is not constructed together with the checkbox icon and put in the checkbox? It this is the case you need to write a lot more code :-)

As for the JCheckBox, there are some methods for getting the state Icons. I believe it is getIcon() but there are also some other for the other states. Once you find the appropriate getIcon methods, you might want to store the Icons somewhere.
I see two ways.
- You can store all the icons you need once, in some static members.
- You can store them in every call the the getCellRendererComponent. Appropriate place for the storage is your model structure. Just put a member for each Icon you want to be stored.

Success,
  Nikolay

------------------------------------
diakov@theoffice.net
------------------------------------

0
 
LVL 5

Author Comment

by:msmolyak
ID: 1222855
Actually it's a bit more complicated than that. The getIcon() method will return null unless you set your own icon. The icon drawn by the Basic checkbox renderer is not aceessable to the checkbox user. Moreover, it is not a gif file, the icon gets drawn on the fly.

I found a solution to my problem (it is clumsy but works). In my FoderCheckBoxIcon class I simply draw a composite icon (in paintIcon() method), that is, first I draw a node icon, and then the checkbox icon.
0
 
LVL 8

Expert Comment

by:diakov
ID: 1222856
I thought so.

CU around.
0
 
LVL 8

Expert Comment

by:diakov
ID: 1222857
I mentioned my e-mail above. E-mail me if you feel like socializing :-)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question