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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:03:59+00:00 2026-05-31T20:03:59+00:00

I have one frame in which one TestArea is there. When I append some

  • 0

I have one frame in which one TestArea is there. When I append some string from this class then String is appended but when I want to append String from other class then String is not appended. I created one method to append string in TextArea, when I call this method in this class then string is appended on text Area. But when I call this method from other Class then String is not appended on TextArea.

Code (MainClass):

public class MainClass {
private JFrame frame;
private TextArea textArea;
private Font font;
private JButton button1;
private JButton button2;
private SecondClass secondClass;

public MainClass() {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        frame = new JFrame("XXX");
        frame.setBounds(200, 200, 600, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        button1 = new JButton("Button1");
        font = new Font("Arial", Font.BOLD, 13);
        button1.setFont(font);
        button1.setBounds(4, 4, 289, 30);

        button2 = new JButton("Button2");
        button2.setFont(font);
        button2.setBounds(300, 4, 289, 30);

        font = null;

        textArea = new TextArea();
        textArea.setBounds(4, 38, 585, 322);
        textArea.setEnabled(true);

        font = new Font("Arial", Font.PLAIN, 13);
        textArea.setFont(font);

        frame.add(button1);
        frame.add(button2);
        frame.add(textArea);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                textArea.append("*** I am in actionPerformed() ***\n");
                appendToTextArea("Call from actionPerformed() method\n");
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                secondClass = new SecondClass();
                secondClass.printOnTextArea();
            }
        });

    } catch (Exception e) {
        textArea.append(e.toString());
    }
}

public void appendToTextArea(String str) {
    System.out.println(str+"\n");

    textArea.append(str+"\n"); //this line not work when I call this method from other class
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            MainClass window = new MainClass();
            window.frame.setVisible(true);
        }
    });
}

}

Code(SecondClass):

import com.grissserver.MainClass;

public class SecondClass extends MainClass{
void printOnTextArea() {
    System.out.println("*** printOnTextArea() ***");
    super.appendToTextArea("call from Second Class in printOnTextArea()");
}
}

Please give some Idea, why this is not working.

  • 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-31T20:04:01+00:00Added an answer on May 31, 2026 at 8:04 pm

    The SecondClass has its own textArea. So you may need to pass MainClass‘s textArea to SecondClass.

    public class SecondClass {
        private TextArea tArea;
        SecondClass(TextArea ta) {
            tArea = ta;
        }
        void printOnTextArea() {
            System.out.println("*** printOnTextArea() ***");
            tArea.append("call from Second Class in printOnTextArea()");
        }
    }
    

    You should change your MainClass like this.

    ....
    button2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            secondClass = new SecondClass(textArea);
            secondClass.printOnTextArea();
        }
    });
    ....
    

    hope this helps…

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

Sidebar

Related Questions

We have some web pages which have two frames, with one frame in the
I have a Manager class and also one frame which isReport .In the frame
Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
I have a dataframe with numeric entries like this one test <- data.frame(x =
In my java-swing project. I have one frame in which user can drop the
Greeting, I have problem stopping FLVPlaypack component when I navigate from one frame to
I have the following question: I have data frame which looks like this. I
I have requirement like this, Have One NSIMageView and which will be changing frequently
We have one existing web application which was running in IE browser. But because
I have code which gets frames from a camera and then saves it to

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.