Search code examples
javanetbeansnetbeans-platform

Dynamically change icon of a widget in NetBeans


I am currently developing a plugin for NetBeans (in short the goal is to integrate my company's tool to NetBeans), and I thought it would be good to have a little widget on the widget bar to tell if my tool is enabled or not.

I saw Geertjan blog post regarding widget buttons, but what he does is enable/disable the button, what I want to do is to change the icon to notify the user of the tools status, and when he clicks on it turns on/off the tool.

Is it doable on NetBeans?

I tried to change layer.xml iconbase attribute to a method to dynamically change this but it doesn't seem to work, at least with when I tried (<attr name="iconBase" stringvalue="TWIconWidget.CTCIcon.getIconPath"/>)

I also tried with an ImageWidget cast on the actionPerformed() of a widget but I can't find a way to see it in the widget toolbar.

Do you have any tips on how to do this?

Thanks in advance

EDIT : If it is not possible, can you offer some alternatives to it, such as a slide button or something that could suit the needs and be visible on the widget bar


Solution

  • This post shows a way to do it (credits to the author Hermien Pellissier): http://www.pellissier.co.za/hermien/?p=646

    Basically you create an Action using the Netbeans wizard and modify it to extend from AbstractAction and implement Presenter.Toolbar.

    You can then override getToolbarPresenter() to return any Component you want to show in the toolbar, for example a JButton or a custom component.

    Using the Component methods you can change its appearance, for example:

    public final class SomeAction extends AbstractAction implements Presenter.Toolbar {
    
        private JButton button;
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // change the button image
            ImageIcon icon = new ImageIcon("another_image.png");
            button.setIcon(icon);
        }
    
        @Override
        public Component getToolbarPresenter() {
            ImageIcon icon = new ImageIcon("image.png");
    
            // create the button to show
            button = new JButton(icon);
            button.addActionListener(this);
    
            return button;
        }
    }