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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:09:54+00:00 2026-05-28T00:09:54+00:00

I am trying to create a custom event which listens and responses to the

  • 0

I am trying to create a custom event which listens and responses to the clicks of a button. The button is represented as an oval, drawn in a paintComponent method on shown in a JFrame. When it is clicked, it is supposed to change colour, then return to a previous colour when clicked again – like an ON / OFF button.

I have created an interface for it:

public interface gListener {
    public void gActionPerformed(GooEvent e);
}

An event listener:

public class gEvent extends java.util.EventObject
{
    private int x, y;
    private int value;
    private gComponent source;
    final static int ON = 1;
    final static int OFF = 0;

public gEvent(int x, int y, int val, gComponent source)
{
    super(source);
    this.x = x;
    this.y = y;
    value = val;
    this.source = source;
}
}

A component class to represent the button:

public abstract class gComponent {

    //Link an arraylist to the gListener interface.
    ArrayList <gListener> listeners = new ArrayList<gListener>();
    int x, y, w, h;

    public gComponent(int x, int y, int w, int h) {
        this.x = x; this.y = y; this.w = w; this.h = h;
    }

//Add listener to arraylist
    public void addListener(gListener gl)
    {
        listeners.add(gl);
    }

    // Dispatches the gEvent e to all registered listeners.
    protected void send (gEvent e){
        e = new gEvent(x, y, w, this);
        Iterator<GooListener> i = listeners.iterator();
        while (i.hasNext())  
        {
            ((gListener) i.next()).gActionPerformed(e);
        }
    }
}

The gComponent class is extended in a button class (gButton), which is where the paintComponent and mouseClicked methods are called. And lastly… I have the test class which extends a JPanel and implements the gListener interface. The main method looks like the following:

public static void main(String[] args) {
        // JFrame code goes here....


        gButton button = new gButton(20,20,20,20); //Click oval shape

          //Using addListener method from gComponent superclass.
          //The 'this' code is throwing error: cannot use this in a static context.
        button.addListener(this); 
}

  //Cause something to happen - stop/start animation.
    public void gooActionPerformed(GooEvent e){


    }

The event is suppose to trigger from a click of the button, in this particular format of how I have written the code.
Any advice to the error I received as stated in my test class, or anything else would be much appreciated. Many thanks.

  • 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-28T00:09:55+00:00Added an answer on May 28, 2026 at 12:09 am

    This is simply… breathtaking.

    But in any case, there’s no this in a static method like main(). If you want an object with a gooActionPerformed() method, then you need to create an instance of whatever class main() appears in, and use that instance in place of this.

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

Sidebar

Related Questions

I'm trying to create a custom control - a button - which will have
I'm trying to create a custom button where the foreColor is always crimson and
I am trying to create a custom context processor which will render a list
I am trying to create a custom control, which behaves a bit like one
I am trying to create a Custom control specifically for my application which would
Im trying to create a custom version of the RequiredAttribute to replace the built
I'm trying to create a custom UIButton that should look like a UIButtonTypeRoundedRect. In
I'm trying to create a custom component with a collection property. However if I
I'm trying to create a custom MembershipProvider and I was wondering how I would
I am trying to create a custom property in a extended control, so that

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.