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 1086409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:48:34+00:00 2026-05-16T22:48:34+00:00

I am trying to make a GUI in Java, using something along these lines:

  • 0

I am trying to make a GUI in Java, using something along these lines:

public class GUIApp
{
    DrawingPanel dp;
    buttonPanel bp;
    ova = Oval;
 public GUIApp()
    {
        JFrame win = new JFrame("Drawing App");
        dp = new DrawingPanel();
        bp = new buttonPanel(this);

    //Settings for panels and frame

        ova = new Oval(100,100,100,100);

      }
      public void setOval(int c){
        //Change color of oval
      }
  }

Then in my buttonPanel class I have this:

public class ButtonPanel extends JPanel
{

    private JButton btnRed, btnGreen, btnBlue;

    public ButtonPanel(GUIApp d)
    {

        ButtonListener listener = new ButtonListener();
        btnRed = new JButton("Red");
        btnGreen = new JButton("Green");
        btnBlue = new JButton("Blue");

        btnRed.addActionListener(listener);
        btnGreen.addActionListener(listener);
        btnBlue.addActionListener(listener);

        setBackground(Color.lightGray);
        GridLayout grid = new GridLayout(3,1);
        add(btnRed,grid);
        add(btnGreen,grid);
        add(btnBlue,grid);
    }

    private class ButtonListener implements ActionListener{

        public void clickButton(ActionEvent event) {
            Object location = event.getSource();
            if (location == btnRed){
                d.setOval(1);
            }
            else if(location == btnGreen){
                d.setOval(2);
            }
            else if(location == btnBlue){
                d.setOval(3);
        }
        }

}
}

But netbeans gives an error for the inner ButtonListener class, and I don’t know why. I also don’t know how to correctly call the setOval from within that class to the GUIApp class. What am I doing wrong?

  • 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-05-16T22:48:34+00:00Added an answer on May 16, 2026 at 10:48 pm

    The problem is that when you implement ActionListener you must define the method actionPerformed(ActionEvent e); you haven’t done that in your ButtonListener class. You can’t name the method anything that you want (as you’ve done with clickButton), so you should just rename your clickButton method to actionPerformed (and go ahead and add an @Override annotation too).

    Now in order to call d.setOval from within your inner class, d must be in scope when the actionPerformed method is called. There are a couple ways to achieve this: you could make d a member variable of your class, or you could define your ButtonListener as an anonymous class.

    For example, if you saved d as a member variable then your code would look like this:

    public class ButtonPanel {
     private GUIApp d;
    
     public ButtonPanel(GUIApp d) {
      this.d = d;
      // The rest of your code here...
     }
    }
    

    Or, you could use an anonymous class like this:

    public ButtonPanel(GUIApp d) {
     ActionListener listener = new ActionListener(){
      @Override
      public void actionPerformed(ActionEvent event) {
       Object location = event.getSource();
       if (btnRed.equals(location)) {
        d.setOval(1);
       } else if (btnGreen.equals(location)) {
        d.setOval(2);
       } else if (btnBlue.equals(location)) {
        d.setOval(3);
       }
      }
     };
     // The rest of your constructor code here ...
    }
    

    Note: Notice how I also changed the use of == to equals() for object equality.

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

Sidebar

Related Questions

No related questions found

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.