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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:16:04+00:00 2026-06-01T03:16:04+00:00

It is my understanding that one can handle events through Swing by adding a

  • 0

It is my understanding that one can handle events through Swing by adding a Listener (eg. a MouseListener) using a Component’s addXXXListener method.

Suppose I want, for whatever, to intercept ALL events that a Component (in my case, a JFrame) receives and do something generic with them eg. write my custom event dispatcher for whatever reason. One very cumbersome and slow way to do this would be to create a Listener class that listens for every single event and does something with them.

However, I found a better idea: I extended JFrame, and overrode processEvent(AWTEvent event) like so:

public class GameFrame extends JFrame
{
    public GameFrame(int width, int height)
    {
        super();
        //blah blah, stuff
        setVisible(true);
    }

    //override processEvent which is called in Component class every time an event happens
    @Override
    public void processEvent(AWTEvent event)
    {
        System.out.println("override " + event);
        //do whatever I want with my event here, then send it back up
        doSomething(event);
        super.processEvent(event);
    }
}

The idea is that whenever an event happens on JFrame, processEvent will be called, and I’ll be able to get the event straight from there.

Unfortunately, the processEvent method is only being called with WindowEvents. MouseEvents and KeyEvents don’t seem to be working: when I run the code, the System.out.println only prints out WindowEvents.

I think I know the reason – because there are no MouseListeners in my code, Component doesn’t bother to call processEvent because there are no MouseListeners. So I could get this code to work if I created and added to JFrame a MouseListener, KeyListener…etc but this is no different from cumbersome and slow method I mentioned earlier.

So what can I do? Is there perhaps some other Component method that is called every time an event happens on the component, and which can be overriden in order to intercept all events on the Component directly from the horse’s mouth, without having to use Listeners?

  • 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-01T03:16:05+00:00Added an answer on June 1, 2026 at 3:16 am

    It is because of the JFrame only support two type of Event. In JFrame.java, there is one line:

    protected void frameInit() {
        enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK);
        //other code here.
    }
    

    the solution is like below, change the behaviour of JFrame to force it listene to MouseEvent and other event you name (for exmaple: AWTEvent.MOUSE_MOTION_EVENT_MASK or MOUSE_WHEEL_EVENT_MASK).

    The enableEvents() is in Component class, and it is protected method, so have to walk around by using reflection.

    public static void main(String[] args)
        throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
    {
        JFrame frame = new GameFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setSize(800, 600);
    
        frame.setVisible(true);
    }
    
    public static class GameFrame extends JFrame
    {
        public GameFrame()
        {
            super();
            enableEvent(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK);
        }
    
        //override processEvent which is called in Component class every time an event happens
        @Override
        public void processEvent(AWTEvent event)
        {
            System.out.println("override " + event);
            super.processEvent(event);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It is my understanding that I can test that a method call will occur
It is to my understanding that one should use a forward-class declaration in the
I have a solid understanding of most OOP theory but the one thing that
It's my understanding that StackOverflow (SO) was built using ASP.NET. What surprised me is
I am looking to create a QTcpServer using PyQt that can simultaneously return data
I've read akka documentation and can't produce clean understanding of thread interaction while using
Understanding that I should probably just dig into the source to come up with
It's my understanding that nulls are not indexable in DB2, so assuming we have
It's my understanding that common wisdom says to only use exceptions for truly exceptional
It is my understanding that the default behavior when creating a table in SQL

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.