How to use Java Swing Actions to create menu items and toolbar items in Netbeans

I am reading a java book and the author creates menu items and toolbar items using the same action. How can I do that using NetBeans? Here is the code :

    public class ActionInterfaceDemo extends JFrame{

    private JPanel buttonPanel = new JPanel();
    private FlowLayout flowLayout = new FlowLayout();

    //---Default constructor
    public ActionInterfaceDemo()
    {
        //Create image icons
        ImageIcon leftImageIcon = new ImageIcon(getClass().getResource("image/leftAlignment.png"));
        ImageIcon centerImageIcon = new ImageIcon(getClass().getResource("image/centerAlignment.png"));
        ImageIcon rightImageIcon = new ImageIcon(getClass().getResource("image/rightAlignment.png"));

        //Create actions
        Action leftAction = new MyAction("Left", leftImageIcon, "Left alignment for the buttons in the panel", new Integer(KeyEvent.VK_L), KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
        Action centerAction = new MyAction("Center",centerImageIcon, "Center alignment for the buttons in the panel", new Integer(KeyEvent.VK_C), KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
        Action rightAction = new MyAction("Right", rightImageIcon, "Right alignment for the buttons in the panel", new Integer(KeyEvent.VK_R), KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));

        //Create menus
        JMenuBar jMenuBar1 = new JMenuBar();
        JMenu jmenuAlignment = new JMenu("Alignment");
        setJMenuBar(jMenuBar1);
        jMenuBar1.add(jmenuAlignment);

        //Add actions to the menu
        jmenuAlignment.add(leftAction);
        jmenuAlignment.add(centerAction);
        jmenuAlignment.add(rightAction);

        //Add actions to the toolbar
        JToolBar jToolBar1 = new JToolBar(JToolBar.HORIZONTAL);
        jToolBar1.setBorder(BorderFactory.createLineBorder(Color.red));
        jToolBar1.add(leftAction);
        jToolBar1.add(centerAction);
        jToolBar1.add(rightAction);

        //Add buttons to the button panel
        buttonPanel.setLayout(flowLayout);
        JButton jbtLeft = new JButton(leftAction);
        JButton jbtCenter = new JButton(centerAction);
        JButton jbtRight = new JButton(rightAction);
        buttonPanel.add(jbtLeft);
        buttonPanel.add(jbtCenter);
        buttonPanel.add(jbtRight);

        //Add toolbar to the east and panel to the center
        add(jToolBar1, BorderLayout.NORTH);
        add(buttonPanel, BorderLayout.CENTER);
    }


    //inner class MyAction
    private class MyAction extends AbstractAction
    {
        String name;

        MyAction(String name, Icon icon)
        {
            super(name, icon);
            this.name = name;
        }

        MyAction(String name, Icon icon, String desc, Integer mnemonic, KeyStroke accelerator)
        {
            super(name,icon);
            putValue(Action.SHORT_DESCRIPTION, desc);
            putValue(Action.MNEMONIC_KEY, mnemonic);
            putValue(Action.ACCELERATOR_KEY, accelerator);
            this.name = name;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (name.equals("Left"))
                flowLayout.setAlignment(FlowLayout.LEFT);
            else if (name.equals("Center"))
                flowLayout.setAlignment(FlowLayout.CENTER);
            else if (name.equals("Right"))
                flowLayout.setAlignment(FlowLayout.RIGHT);

            buttonPanel.revalidate();
        }
    }


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        ActionInterfaceDemo frame = new ActionInterfaceDemo();
        frame.setLocationRelativeTo(null);  //Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("ActionInterfaceDemo");
        //frame.pack();
        frame.setSize(new Dimension(600, 200));
        frame.setVisible(true);
    }
}

Thank you.

Answers


Both JMenuItem and JButton have a setAction() method that you can use for setting the same instance of your Action.


Need Your Help

Megento dev site re-directing to live site URL

magento magento-1.7

I'm having a huge issue with accessing my dev site. I two magentos sites on different servers. One is the dev site, the other live. However I needed to copy the database from the live to the dev si...

Getting link.exe, lib.exe, editbin.exe and dumpbin.exe to use the symbol server

debug-symbols pdb dumpbin

I have WinDbg set to download symbols to c:\symbols. The symbols for advapi32 (for example) are in c:\symbols\advapi32.pdb\3F32049F550C42B09CF114A1FB8A97E92\advapi32.pdb. The only way I've gotten

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.