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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:40:29+00:00 2026-05-20T06:40:29+00:00

I’ve got a very silly question to ask. I’m using NetBeans to built a

  • 0

I’ve got a very silly question to ask.

I’m using NetBeans to built a small app and I’m having the following problem;
My main class is called mainApp and is extending a JFrame which in turn contains a JPanel called drawingBoard which I also extend for various (and off-topic) reasons..

The core problem is that at some point I need to access one of the fields of the mainApp but due to the way NetBeans instantiates my main class..(as anonymous class) I can’t get a reference to the container(that is my mainApp).

What can I do to get a reference of mainApp and set the value of its field within drawingBoard?

  • 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-20T06:40:30+00:00Added an answer on May 20, 2026 at 6:40 am

    You can get a reference to the top level window by using Window win = SwingUtilities.getWindowAncestor(myComponent); and passing into the method call a refrence to any component that the top level window ultimately holds. If your main class is also your top level JFrame (you’re not initializing any other JFrames), then you can cast the returned Window to your top level class type and call its public methods.

    For example:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class Foo1 {
       public static void main(String[] args) {
          MainApp mainApp = new MainApp();
          mainApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          mainApp.pack();
          mainApp.setLocationRelativeTo(null);
          mainApp.setVisible(true);
       }
    }
    
    class MainApp extends JFrame {
       public MainApp() {
          getContentPane().add(new DrawingBoard());
       }
    
       public void mainAppMethod() {
          System.out.println("This is being called from the Main App");
       }
    }
    
    class DrawingBoard extends JPanel {
       public DrawingBoard() {
          JButton button = new JButton("Button");
          button.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                MainApp mainApp = (MainApp) SwingUtilities.getWindowAncestor(DrawingBoard.this);
                mainApp.mainAppMethod();
             }
          });
          add(button);
       }
    }
    

    altered to be done by glowcoder’s recommendation:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class Foo2 {
       public static void main(String[] args) {
          MainApp2 mainApp = new MainApp2();
          mainApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          mainApp.pack();
          mainApp.setLocationRelativeTo(null);
          mainApp.setVisible(true);
       }
    }
    
    class MainApp2 extends JFrame {
       public MainApp2() {
          getContentPane().add(new DrawingBoard2(this));
       }
    
       public void mainAppMethod() {
          System.out.println("This is being called from the Main App");
       }
    }
    
    class DrawingBoard2 extends JPanel {
       private MainApp2 mainApp;
    
       public DrawingBoard2(final MainApp2 mainApp) {
          this.mainApp = mainApp;
          JButton button = new JButton("Button");
          button.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                buttonActonPerformed();
             }
          });
          add(button);
       }
    
       private void buttonActonPerformed() {
          mainApp.mainAppMethod();
    
       }
    }
    

    Another recommendation: since you’re learning Swing, you’re far better off not using NetBeans to generate Swing code for you but rather to code your Swing apps by hand. By doing this and studying the tutorials, you’ll gain a far deeper and better understanding of just how Swing works, and it will help you should you need to use the NetBeans code generator to make anything more than the most simple of apps.

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

Sidebar

Related Questions

We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.