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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:52:47+00:00 2026-06-14T23:52:47+00:00

I have two clases GUI ( That renderes my main JFrame ) and Print

  • 0

I have two clases GUI ( That renderes my main JFrame ) and Print class ( That is invoked by JButton on GUI class ). Now on my GUI Class i have JTextArea and a method:

void setOutput(String data)
{
   // output is JTextArea
   output.setText(data);
}

However the data is provided the the Print JFrame where i have a JButton with action listener:

sizOpt.addActionListener(new ActionListener()
{       
    @Override
    public void actionPerformed(ActionEvent event)
    {
       // textfield is a JTextField component
       String data = textfield.getText();   


       // My problem is here i need to invoke the setOutput
       // method in GUI to output the string however i cant call that method in
       // any way but making it static or calling new GUI which will create a new
       // Instance of GUI class
       GUI.setOutput(data);
    }
});
  • 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-14T23:52:48+00:00Added an answer on June 14, 2026 at 11:52 pm

    The answer: don’t use static anything here at all.

    The only thing that should be static is your main method, and that’s probably it. If you need to call a method on the GUI, then call it on an instance of the visualized GUI, not as a static method. Often the tricky part is getting that valid reference, and you are correct that you shouldn’t create a new GUI object, but again don’t try to do a non-working static solution. Some ways to get the valid reference are via a constructor parameter or a setter method.

    i.e.,

    public class PrintJFrame extends JFrame {
      private GUI gui;
    
      public PrintJFrame(GUI gui) {
        this.gui = gui;
      }
    
      // ...
    }
    

    Now in your ActionListener you can call a method on the correct GUI reference held by the gui variable. Next we’ll talk about why you should avoid having classes extend JFrames and similar GUI components.
    Next we’

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

Sidebar

Related Questions

I have two classes, a gui class that contains two combobox's and a listener
Say I have two classes, and neither of them are GUI components. Class A
I have two classes Foo and Bar that Bar extends Foo as below: class
I have two classes, one that inherits from the other. The base class is
I have two classes that don't know anything about themselfs class A, class B.
I have two classes that are derived from an abstract generic class, which is
I have two classes - one that runs in the main thread and performs
I have two classes gameWindow and gameEngine . The Main method is in gameWindow
So my program consists of two classes 1. My main class handling GUI which
I have two classes (MVC view model) which inherits from one abstract base 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.