sandip chitale's blog    Sandip Chitale's blog (scblog)
NOTE: I have moved many of my modules to NetBeans Plugin Portal . Please check there for latest versions of modules described on this blog.
20060129 Sunday January 29, 2006

Toggle action in NetBeans Menubar and Toolbar

In the last entry I talked about Mark Occurrences module. The module makes use of a toggle action to turn the Mark Occurrences feature on and off. The toggle action is represented by a JCheckBoxMenuItem in the Menu and a JToggleButton on the toolbar. Initially for some reason ( I guess I did not look for what I should use hard enough ;) ) I implemented this using a sub class of SystemAction myself and return the JCheckBoxMenuItem and JToggleButton components in public JMenuItem getMenuPresenter() and public Component getToolbarPresenter() respectively. To achive the synchronization of the selected state between the JCheckBoxMenuItem and JToggleButton I had to do the gimmick of sharing the ButtonModel etc. And then I came across BooleanStateAction. Here is how it works:

The BooleanStateAction provides a pair of methods to get and set the state of the action - public boolean getBooleanState() and public void setBooleanState(boolean snewSate). It also provides UI components. You subclass BooleanStateAction and provide the labels and icons as usual. You do not override the usual actionPerformed() or performAction() methods. Instead you add a PropertyChangeListener to the action. The property change event callback looks like this:

    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(BooleanStateAction.PROP_BOOLEAN_STATE)) {
            // make use of the action state return by getBooleanState()
        }
    }

By default the BooleanStateAction sets the boolean state to true. To start with the initial state of false override the initilize() method like this:

    protected void initialize() {
        super.initialize();
        setBooleanState(false);
    }

Thats all.

Posted by sandipchitale ( Jan 29 2006, 02:37:04 PM PST ) Permalink Comments [0]










January 2006 »
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
20
28
    
       
Today

Get NetBeans 5.5

Locations of visitors to this page

Today's Page Hits: 367


XML
All
/Creator
/General
/Hobby
/Java
/JavaScript
/Mozilla
/NetBeans
/Ubuntu
/VisualWeb
/VisualWebPack
/Web 2.0

XML
All
/Creator
/General
/Hobby
/Java
/JavaScript
/Mozilla
/NetBeans
/Ubuntu
/VisualWeb
/VisualWebPack
/Web 2.0

scblog
scblog