Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8539997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:29:57+00:00 2026-06-11T11:29:57+00:00

I am reading a java book and the author creates menu items and toolbar

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:29:58+00:00Added an answer on June 11, 2026 at 11:29 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Java and it says that you can declare
I'm reading Programming for Java Virtual Machine by Joshua Engel book where the author
I am reading this book called Java Concurrency in Practice and the author gives
I'm reading Beginning Java Game Programming 2/e and the book heavily emphasizes using AffineTransform.
I am reading a book that is on android but brushes over some java
Theres an exercise from a Java book I'm reading that has me confused: A
I am reading Josh Bloch's book Effective Java and he suggests using a builder
I'm learning Java by reading the online Java book, but im finding it hard
I am reading up on multi-threaded programming in Java. In the java book it
I'm reading a book about java. It just got to explaining how you create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.