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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:19:13+00:00 2026-06-11T16:19:13+00:00

I have a Swing-GUI and an external class. In the constructor of the Swing

  • 0

I have a Swing-GUI and an external class.
In the constructor of the Swing GUI I instantiate a new object of the external class.
But I can’t use this object from other methods of the GUI-class (e.g. within an action listener). If I instantiate the object directly in the action listener then I can use all methods of the external class.

Here are the relevant snippets of code; if you need more tell me 🙂

1) my External class

public class ExternalClass
{
    private int a = 100;
    public int getA() {
        return a;
    }
}

2) parts of my GUI-class

public class GUI extends javax.swing.JFrame
{

// constructor
public GUI()
{
    initComponents();
    ExternalClass e = new ExternalClass();
}
//...
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{

    int u = e.getA();
// this doesn't work - the object e is not known by the method

}
//...
        java.awt.EventQueue.invokeLater(new Runnable()
    {

        public void run()
        {
            new GUI().setVisible(true);

        }
    });
  • 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-11T16:19:14+00:00Added an answer on June 11, 2026 at 4:19 pm

    The object is declared in the constructor. Thus, it only exists within the constructor itself. If you want to use it in other methods you must declare it outside, as an attribute of the class, e.g. like this.

    ExternalClass e;    
    
    // constructor
    public GUI()
    {
        initComponents();
        e = new ExternalClass();
    }
    

    Notice that this field will be visible to all the classes in the package that contains your GUI class. You might want to specify access level (private, public or none for package access).

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

Sidebar

Related Questions

I am creating one GUI in swing Java. I have to use one button
I am trying to create a GUI using java swing. From there I have
I'm developing Swing application, and everything works fine usually. But I have an GUI
I have a swing application that uses some datas coming from an external mysql
Try to use the FEST-Swing for Swing GUI testing, and using example from http://easytesting.org/swing/wiki/pmwiki.php?n=FEST-Swing.LaunchFromMain
I have a SWING GUI class that instantiates a custom JPanel for a portion
I'm creating a Java swing GUI and I have formatted a JPanel to use
I have got a Java-Swing-GUI with a structure like this: JFrame -- JPanel1 -----jButton1
I have made a java swing GUI. Now I want to display a static
I have to setup a basic JAVA application including a GUI based on swing.

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.