开发者

Is it okay to have JCheckBox within JButton in Java?

开发者 https://www.devze.com 2023-03-30 09:30 出处:网络
I have a uploader program that has a JTable displaying a set of rows of uploads. I have a JButton labeled \"Clear Completed\" which when clicked removes the rows of completed uploads. Now I\'d like to

I have a uploader program that has a JTable displaying a set of rows of uploads. I have a JButton labeled "Clear Completed" which when clicked removes the rows of completed uploads. Now I'd like to have a JCheckBox/JButton combination (like the chec开发者_StackOverflow社区kbox/dropdown combination in gmail) so that if the checkbox is checked, the rows will be removed automatically.

I have created a class that extends JButton and overridden the constructor to add a JCheckBox. Added ActionListeners for both JCheckBox and JButton. It looks like this.

http://i.stack.imgur.com/KcBHv.png

Everything works perfectly. But I'm in a dilemma whether to use it or not. Since it's not an intended use of JButton API and I've never seen such control (so far) in any Java app so I'm afraid if it'll cause any problems for users.

Will this cause any problems like breaking UI during runtime? Or is it possible it won't work in all OSes? (I'm using Windows7)

Or it is not good designing approach? ("Users must not be surprised" rule.. I guess they'll understand. I've also included a tooltip for that checkbox)

Please share your opinions. Thank you.


Will this cause any problems

Maybe. You can, for example, get rendering problems if a native widget is used to render the button. Or the checkbox might not respond to clicks because the button swallows the event.

Is it a good designing approach?

Not really. Most users will think that your layout has a bug because they've never seen a checkbox inside of a button before. Along with any rendering issues (what will the checkbox look like when I press the button? Will it vanish or render in odd colors?), I feel that you should avoid this.

Use the standard approach: Checkbox with text ("Clear automatically") above button. Maybe change the button text when the checkbox gets selected to "Close".


Will this cause any problems like breaking UI during runtime? Or is it possible it won't work in all OSes? (I'm using Windows7)

I think this won't be a problem.

Or it is not good designing approach? ("Users must not be surprised" rule.. I guess they'll understand. I've also included a tooltip for that checkbox)

Personally I think that putting the checkbox at left of the JButton would be more clear. Anyway it's up to you. In addition, quick movements performed may let the user click on the button rather than on the checkbox.


In case any one is wondering how you might do this. You will probably have to add an EmptyBorder(4,4,4,4) to keep the size similar to that of a button. I usually wrap them in a grid layout with other buttons.

            new JCheckBox() {
            JButton button = new JButton();
            {
                button.setModel(getModel());
            }

            @Override
            protected void paintComponent(Graphics g) {
                if (!button.getSize().equals(getSize())) {
                    button.setSize(getSize());
                }

                button.paint(g);
                super.paintComponent(g);
            }

            @Override
            public void setEnabled(boolean b) {
                super.setEnabled(b);
                button.setEnabled(b);
            }
        };
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号