Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

appending message in TextArea  RSS feed

Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Class A contains a text area:

I have an message that needs to be displayed in that text area, but it is generated by code in Class B. I got an error message that I cannot use a non-static variable in a static context, so I created an instance of Class A and its text area within Class B :

A button triggers the cancel() function in Class B. I know that works because the system will print out the message, but the text area will not. What am I missing?

thank you!
Posts: 825
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you can compile this? What's the event in your method, I can't see you declared it and what is the purpose of that cancel() method on event object?

I suppose that A reference in your B class is not the frame you are actually displaying to the user and the one that contains the button handling the event. So what you're actually doing is just create new A frame, do something with it, but since it's not the one that is displayed you can't see the result of the action. It's a variable local to that method in B class.

Why don't you declare method in class B to return a String (message that's to be appended to your JTextArea)? Invoke that method on object of B class from event handler in class A to get the message as String and append it where needed.
I'm full of tinier men! And a tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!