• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access an Object from another file [Swing GUI]

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

got a very simple question which I currently have no idea how to solve (thats why I'm posting it now

I build an action listener for a button, but I used a separate file for this (class MiniGuiActionListener). So far so good. But now, when the button was clicked, I want to access the jButton object from within the actionPerformed method. How can I accomplish this? (I just want to invoke something like jButton.setText(...) ).

Thanks in advance,
Steffen

I got these two simple Java files (with all imports etc.):


Second file:



 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
I shall move this discussion to our GUIs forum.
If the button is a local variable in that method, it ceases to exist outside the method, unless you pass a reference to it elsewhere. Why not pass a reference to the constructor of the listener class, and keep it as a field of the listener class? Then you can access it inside the listener class.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see it is a field. You ought to have given it private access. You can pass it as an argument to eht Listener class’s constructor just the same as for a local variable.
 
Steffen Lima
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfect - passing an object reference to the constructor just solved my little "problem"

Thank you very much !!
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
Did you miss me? Did you miss this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic