Monday, November 29, 2010

JSplitButton

A simple implementation of the split button control in Java. This control raises two events
  1. buttonClicked(e) 
  2. splitButtonClicked(e)
 The buttonClicked event is raised when the button is clicked, the left part, which will not trigger the dropdown menu. Whereas the splitButtonClicked event is raised when the split part of the button is clicked and displays a popup menu.
To handle these events you need to subscribe to SplitButtonActionListener.

Screenshots:






Using the Control:
//first instantiate the control
JSplitButton splitButton = new JSplitButton();
//register for listener
splitButton.addSplitButtonActionListener(new SplitButtonActionListener() {

            public void buttonClicked(ActionEvent e) {
System.out.println("Button Clicked");
            }

            public void splitButtonClicked(ActionEvent e) {
System.out.println("Split Part licked");
            }
        });
//add popup menu
splitButton.add(popupMenu);
//add this control to panel
panel.add(splitButton);

NetBeans users can also add the control to the NetBeans palette.

Known Issue:
The 'button part' of the splitbutton is being drawn without the border??? and this is only happening in CDE/Motif and Metal Look and Feels.GTK+ and nimbus works perfect.(See Screenshot below) No Idea why? if anybody could point out the mistake that'd be nice.


The source for this control can be downloaded from google-code.

No comments:

Post a Comment