CheckyButton! A combination of JCheckBox and JButton

I liked the checkbox+pulldown menu combination in Gmail. I have been dreaming of creating fascinating new type of controls including this one even before it came out. Last night I have been thinking about how to add a requested feature in Neembuu Uploader that clears the list of rows of completed downloads whenever the user wishes or always automatically. I got this idea for such control that combines both the actions of a button and checkbox in a single control.

Something like this:
Checky2

And creating it in Swing is very simple. Just extend the JButton and add a JCheckBox into it. As simple as that!

 

 

Create a CheckyButton class like this:

Checkybutton
Then in your frame or other class, import this class and construct it like a normal JButton(String text).

//Add our Checky Button

        final CheckyButton cb = new CheckyButton("Clear Completed");

        cb.setSize(100,20);

Then add the action listeners for both Button part and Checkbox part of our CheckyButton

//Add an Action Listener for "Button" part of CheckyButton

        cb.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                JOptionPane.showMessageDialog(null, "JButton Clicked :)");

            }

        });

 

        //Add an Action Listener for "Checkbox" part of CheckyButton

        cb.getCheckBox().addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                if(cb.getCheckBox().isSelected())

                    JOptionPane.showMessageDialog(null, "Checkbox selected");

                else

                    JOptionPane.showMessageDialog(null, "Checkbox not selected");

            }

        });

And finally add this CheckyButton to your frame or any other container using their add(Component comp) method.

Done!

This is a test screenshot of one of the possible ways Neembuu Uploader might look like in next version:

Neembuuuploader2

 

Posted via email from Art, Science & Technology