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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:37:04+00:00 2026-06-12T14:37:04+00:00

I have a JFrame application with some variables and a number of SWING widgets.

  • 0

I have a JFrame application with some variables and a number of SWING widgets. In it I create an instance of another class and pass the JFrame to the child in the constructor. From the child, I can reference the variables, but not the widgets. Why?

 // My JFrame
 public class Prot2Prom extends JFrame  {

    // My Child
    public Prot2Prom() {
    super( "Protocol To PROM" );
        Child child = new Child(this);

In the Child class my constructor does

 Prot2Prom frame = null;

 public Child(Prot2Prom gui) {
    frame = gui;
 }

The following works:

 frame.<parent variable>=x;

The following does not:

 frame.textArea.append("Hello");

The textArea cannot be resolved. There were all added with “new”. Why can’t I see them?

  • 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-12T14:37:05+00:00Added an answer on June 12, 2026 at 2:37 pm

    Some notes and recommendations:

    • This has nothing to do with “widgets” or Swing and all to do with visibility of variables. I’m guessing that textArea is not a public field of the Prot2Prom class.
    • If variables are public outside classes can “see” them, access them, modify them.
    • A possible solution is to in fact make the variables that you want other classes to see, public.
    • In general you really don’t want to do this.
    • Instead much better is to give a class public methods that allow other classes to call and by doing so alter the original class’s behavior. In other words, your Swing GUI classes should adhere to good OOPs principles just as any Java class should.
    • Later you’ll want to read up on the MVC or Model, View, Control design pattern as a way of separating out behaviors of your code into separate logical entities, which can make your code much more flexible and powerful.

    Edit 1
    Regarding your comment:

    The “widgets” are all created by WindowsBuilder Pro. I am trying to use the textArea to create my Eclipse Console for a stand alone (jar) application. How can I print to it from a class instantiated by the Frame?

    You’ll want to give the class that holds the textArea variable a public method:

    public void appendTextAreaText(String text) {
      textArea.append(text);
    }
    

    Then your other classes can append text to the JTextArea. Why is this important? One reason is that if the class that holds textArea will at some times not want to allow other classes the ability to append to this widget, it can have the logic to control this in the method. Thus it gives much more control over the widget to the class that holds it.

    e.g.,

    public void appendTextAreaText(String text) {
      if (allowTextAreaAppend) {  // a class boolean field 
        textArea.append(text);
      }
    }
    

    As an aside, I also recommend that you put the code generation tool to the side and instead create your Swing GUI’s by hand for a bit until you get a firm grasp of Swing and Java fundamentals. This will make your future use of the Swing code generation tool much better and productive.

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

Sidebar

Related Questions

I am building a swing application. At some point, I have to start an
I have developed a Java Swing application, which uses the SwingWorker class to perform
I have a netbeans Java application that should display a JFrame (class StartUpWindow extends
I'm having some trouble designing an MDI Application with Swing. I have no trouble
I have found an open source application that creates a JFrame to display some
i'm developing a java application in which i have a JFrame with a JDesktopPane
I have an application that is works fine and the JFrame for it is
I have a JFrame with some a JTextField and a JButton. I want it
I have a JFrame and I open a JDialog from it and another JDialog
I have a JFrame which holds and JScrollPane. The JScrollPane itself holds a class

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.