Search code examples
javaswinguser-interfacejbuttonjcheckbox

Is it okay to have JCheckBox within JButton in Java?


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 checkbox/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.

https://i.sstatic.net/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.


Solution

  • 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".