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

  • Home
  • SEARCH
  • 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 8758875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:39:42+00:00 2026-06-13T14:39:42+00:00

I have these classes: a JPanel extension, an interface and 3 x JmenuItem classes.

  • 0

I have these classes: a JPanel extension, an interface and 3 x JmenuItem classes.

public class RedFrame extends javax.swing.JFrame implements ActionListener {
private JMenuBar jMenuBar1;
private JPanel jPanel1;
private fileExitCommand jMenuItem3;
private fileOpenCommand jMenuItem2;
private btnRedCommand jMenuItem1;
private JMenu jMenu1;

/**
 * Auto-generated main method to display this JFrame
 */
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            RedFrame inst = new RedFrame();
            inst.setLocationRelativeTo(null);
            inst.setVisible(true);
        }
    });
}

public RedFrame() {
    super();
    initGUI();
}

private void initGUI() {
    try {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        {
            jPanel1 = new JPanel();
            getContentPane().add(jPanel1, BorderLayout.CENTER);
        }
        {
            jMenuBar1 = new JMenuBar();
            setJMenuBar(jMenuBar1);
            {
                jMenu1 = new JMenu();
                jMenuBar1.add(jMenu1);
                jMenu1.setText("Meniu");
                {
                    jMenuItem1 = new btnRedCommand(jPanel1, "RED");
                    jMenu1.add(jMenuItem1);

                }
                {
                    jMenuItem2 = new fileOpenCommand("Open");
                    jMenu1.add(jMenuItem2);

                }
                {
                    jMenuItem3 = new fileExitCommand("Exit");
                    jMenu1.add(jMenuItem3);

                }
            }
        }
        jMenuItem1.addActionListener(this);
        jMenuItem2.addActionListener(this);
        jMenuItem3.addActionListener(this);
        pack();
        setSize(300 * 16 / 9, 300);
    } catch (Exception e) {
        // add your error handling code here
        e.printStackTrace();
    }
}

@Override
public void actionPerformed(ActionEvent event) {
     Execute();

}

 }

And

public class btnRedCommand extends JMenuItem implements Command {

protected JPanel p;
protected String text;

public btnRedCommand(JPanel p, String text) {

    p.setBackground(Color.cyan);
    this.setText(text);
}

public void Execute() {
    // TODO Auto-generated method stub
    p.setBackground(Color.red);
}

}

and

public interface Command  {

public void Execute();

}

I want the Execute method which is implemented in the 3 JMenuItems to be called depending on which jMenuItem from Menu was selected. How can I properly do this? Do I need a wrapper class for the 3 jMenuItems?

  • 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-13T14:39:43+00:00Added an answer on June 13, 2026 at 2:39 pm

    This pattern is overkill here for these simple GUI tasks, but in your ActionListener, you could do:

    Command command = (Command) event.getSource();
    command.Execute();
    

    Explanation: As each of custom JMenuItem implements the Command interface, they can be cast as such & thereby avail of the Execute method.

    The reason that an NullPoinerException is occurring is that the JPanel instance is not assigned in the Command constructor:

    public btnRedCommand(JPanel p, String text) {
       this.p = p;
       ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the problem I have these classes: Public MustInherit Class BaseLeaf(Of T) Implements IBaseLeaf
I have these classes in C# (.NET Framework 3.5) described below: public class Base
Concerning data binding I have these classes: public class Foo : List<Bar> { public
I have these two classes public class Person { } public class Company {
Let's say we have these two classes: public class Base { public static int
I'm developing an application which will have these classes: class Shortcut { public string
I have these classes: first: class C { public: C(const C& c):_s(c._s){} c():_s(){} string
I have these classes: public abstract class CustomField { public String Id { get;
I have these classes for my Company Entity public class Company { public Company()
I have these classes: public class Person { public int Id{ get; set ;}

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.