Monday August 03, 2009
How to Migrate a Toolbar to the NetBeans Platform (Part 1)
The question of the day comes from Auguste Lunang:
"Ï have a toolbar from my swing application. To migrate it to the netbeans platform how can i just add my existing toolbar to netbeans platform's toolbar ? should i add buttons one by one ?"
So, there are a couple of things that are worth mentioning before showing a small example:
- You can keep your ActionListeners, i.e., no need to change them to anything special, since the NetBeans Platform understands what an ActionListener is.
- You need to register your ActionListeners in your NetBeans Platform application's filesystem. A filesystem is something that each NetBeans Platform application has and if that is news to you, you definitely need to watch the screencast series Top 10 NetBeans APIs.
- There is a template in NetBeans IDE that you can use to register your ActionListeners into the application's filesystem.
That's all good news. So, here's my small application that we'll migrate to the NetBeans Platform:
package demo;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;
public class CustomerJFrame extends JFrame {
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("JavaApplication1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.setSize(new Dimension(400, 300));
frame.setVisible(true);
}
private static void addComponentsToPane(Container contentPane) {
JToolBar toolBar = new JToolBar();
JButton button1 = new JButton();
button1.setIcon(new ImageIcon("src/demo/button1.png"));
button1.addActionListener(new Button1ActionListener());
toolBar.add(button1);
JButton button2 = new JButton();
button2.setIcon(new ImageIcon("src/demo/button2.png"));
button2.addActionListener(new Button2ActionListener());
toolBar.add(button2);
contentPane.add(toolBar, BorderLayout.PAGE_START);
}
}
Notice that our two buttons each have an ActionListener. Here's their simple definition:
package demo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
class Button1ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hello from button 1");
}
}
package demo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
class Button2ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hello from button 2");
}
}
The above code gets you something like the following:
And, in NetBeans IDE, the source structure is as follows:
Now we want to use the NetBeans Platform. The above buttons need to be moved. How? As follows:
- Create your NetBeans Platform application skeleton via the related project template.
- Create the module where the buttons will be moved to via the related project template.
- Copy the two ActionListeners into the module's source structure. (Make sure that their access modifiers are set to public.)
- Use the New Action file template to create a new ActionListener and then delete it. In the module's layer.xml file you'll find the registration entries for the deleted ActionListener. Modify them to match the class names of your own ActionListeners.
You'll end up with something like this, which registers your ActionListeners into the toolbar of your choice, together with the required icons and other information:
<folder name="Actions"> <folder name="Build"> <file name="org-demo-ui-Button1ActionListener.instance"> <attr name="SystemFileSystem.localizingBundle" stringvalue="org.demo.ui.Bundle"/> <attr name="delegate" newvalue="org.demo.ui.Button1ActionListener"/> <attr name="displayName" bundlevalue="org.demo.ui.Bundle#CTL_Button1ActionListener"/> <attr name="instanceCreate" methodvalue="org.openide.awt.Actions.alwaysEnabled"/> <attr name="iconBase" stringvalue="org/demo/ui/button1.png"/> <attr name="noIconInMenu" boolvalue="false"/> </file> <file name="org-demo-ui-Button2ActionListener.instance"> <attr name="SystemFileSystem.localizingBundle" stringvalue="org.demo.ui.Bundle"/> <attr name="delegate" newvalue="org.demo.ui.Button2ActionListener"/> <attr name="displayName" bundlevalue="org.demo.ui.Bundle#CTL_Button2ActionListener"/> <attr name="instanceCreate" methodvalue="org.openide.awt.Actions.alwaysEnabled"/> <attr name="iconBase" stringvalue="org/demo/ui/button2.png"/> <attr name="noIconInMenu" boolvalue="false"/> </file> </folder> </folder> <folder name="Toolbars"> <folder name="File"> <file name="org-demo-ui-Button1ActionListener.shadow"> <attr name="originalFile" stringvalue="Actions/Build/org-demo-ui-Button1ActionListener.instance"/> <attr name="position" intvalue="0"/> </file> <file name="org-demo-ui-Button2ActionListener.shadow"> <attr name="originalFile" stringvalue="Actions/Build/org-demo-ui-Button2ActionListener.instance"/> <attr name="position" intvalue="10"/> </file> </folder> </folder>
When you run the application, you'll see something like this:
The buttons appear in the toolbar, along with the default NetBeans Platform buttons. You can delete those if you don't need them. And here's the source structure of my migrated application:
By the way, it is very easy to provide icons for enabled/disabled/rollover/larger states for your button.
Aug 03 2009, 05:07:06 AM PDT Permalink
Hi Geertjan,
Thank you for your explanations. following steps of your tutorial, i now have a customized toolbar on my netbeans platform.
Another question arised when doing that.
How can i display that toolbar only when a specific window is active ? the toolbar is useless in other windows.
Thx
Posted by Lunang on August 03, 2009 at 06:01 AM PDT #
Like I said on the mailing list: http://blogs.sun.com/geertjan/entry/toolbar_configurations
Posted by Geertjan on August 03, 2009 at 07:05 AM PDT #
Hi Geertjan,
What happens in regards to buttons which need multiple ActionListeners, I don't believe you can have multiple "delegate" attributes?
Also I have wondered about using the filesystem to hook into or override existing Action's ActionListeners. Using the above context for example if I was to create another module and add
<folder name="Actions">
<folder name="Build">
<file name="org-demo-ui Button1ActionListener.instance">
<attr name="delegate" newvalue="MyButton1ActionListener"/>
</file>
This be used to override the existing delegate ActionListener, or if that would open a can of worms, perhaps the above delegate would be added to a set of ActionListeners?
Posted by Caza Henha on August 03, 2009 at 08:25 AM PDT #


