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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:14:25+00:00 2026-05-25T11:14:25+00:00

If I’m creating a class, and I will load an object of that class

  • 0

If I’m creating a class, and I will load an object of that class in my JFrame (the class is basically a panel with button and text objects, but that doesn’t matter much) that isn’t instantiated until my public static void main(String[] args) { (which is below the class code), how do I associate WindowListener and other listeners to that JFrame since it’s not apart of the original class?

Normally when I have this issue Eclipse tells me to make that JFrame or other object static and call it universally, but I tried that and I don’t think it worked with an instance of JFrame.

I’ve read through http://download.oracle.com/javase/tutorial/uiswing/events/windowlistener.html and other tutorials but I want a general answer as much an an answer to this specific question for my app, because I run into this a lot.

Thank you to anyone who can help or anyone who reads this!

  • 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-25T11:14:26+00:00Added an answer on May 25, 2026 at 11:14 am

    Showing us your existing code would make it clearer what you are trying to do, but it sounds as if you have one class that both represents your panel and also contains your main method. That is possible but I would not recommend it because it obscures the structure of the application, although it is a popular technique in tutorials because it enables everything to fit into one file.

    A WindowListener represents any object that needs to respond to window events, i.e. changes in the state of the window (a JFrame in this case) when it is activated, iconified etc. The listener may also represent a graphical component but need not do so.

    Here is a very simple example that I hope will illustrate the concepts. Let’s create a class representing a type of panel containing a JLabel that will display the number of window events that have occurred so far. It will also implement WindowListener so that it can be notified of these events and increment a counter each time one occurs.

    You should be able to compile and run this code as it is, and then watch the counter change if you minimize/maximize the window, click on other windows and so on.

    import java.awt.event.*;
    import javax.swing.*;
    
    public class TestPanel extends JPanel implements WindowListener {
        private JLabel label = new JLabel("No window events yet");
        private int numEvents = 0;
    
        public TestPanel() {this.add(label);}
    
        private void update() {
            label.setText(String.format("%d events",numEvents));
        }
    
        public void windowOpened(WindowEvent e) {
            numEvents++;
            update();
        }
    
        // ... similar implementations of the other WindowListener methods ...
    }
    

    Then we need a main program to instantiate one of our panels and display it in a JFrame.

    import javax.swing.*;
    
    public class Main {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndDisplayGui();
                }
            });
        }
    
        private static void createAndDisplayGui() {
            JFrame frame = new JFrame("Test Frame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            TestPanel panel = new TestPanel();
            frame.add(panel);               // add the panel as a component in the frame
            frame.addWindowListener(panel); // add the panel as a listener to the frame
            frame.pack();                   // lay out and size the frame
            frame.setVisible(true);         // display the frame
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am doing a simple coin flipping experiment for class that involves flipping a
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has

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.