Something for the weekend
Introducing JMultiButton
So this isn't a very "Rich" component yet, but I've been working on a little helper app for myself and I've always needed one of these. It's basically a button that allows you to add multiple actions which can be selected from or the left side clicked to fire the current action. It saves space on a tool-bar. Although I've uploaded it to the cvs repository there is much more work to do, but it is useful right now, which is good!


multi_button

Basically all the component does is extend JButton, over-riding a few key methods. The first thing it does is add something to hold the multiple actions, and identify the selected action.

It then adds a certain amount on to the returned minimum size etc to ensure it has enough space to draw the down arrow on the right (which allows the multiple selection), and of course overrides the paint method to draw the triangle (this needs some real work and cross L&F testing right now) and a line between the triangle and the action when the mouse is hovering.

That line tells the user that clicking on the left will have a different effect to clicking on the right. In order to deliver that we simple check to see if they should fire the current action (on the left) or show a menu of all of the actions (on the right).

/**
* Overridden so that a check can be made to see if the current action should be fired, or the
* list of available actions should be shown.
* @param actionEvent The action event
*/
protected void fireActionPerformed(ActionEvent actionEvent) {
if (this.getMousePosition().x>boundary){
JPopupMenu popup = new JPopupMenu();
for (Action action : actions){
popup.add(new ActionOptionWrapper(action));
}
popup.show(this,0,getHeight());
} else {
super.fireActionPerformed(actionEvent);
}
}
And that's it. The ActionOptionWrapper just sets the selected action as the current action, and then fires it. Simple really! Any how, I'll develop this one and I do have "Rich" plans for it, but given this one can be useful out the box, and I haven't had a chance to do cross platform testing (hint hint).

|