• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with access from ActionListener  RSS feed

 
Rushan Tugushev
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone !

I have a problem:
I have a simple SWING program with buttons and I can't call "frame2.setVisible(true)" from ActionListener registered on a button. I don't understand, why the listener can't see "frame2".




 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
frame2 is a local variable in the main() method, in class test. Local variables are not accessible outside the method in which they are declared.

You'll have to create a member variable of type JFrame in class Vostok and a constructor that takes a JFrame, that sets the member variable. In line 56, in the main() method, you'll have to pass frame2 to the Vostok constructor. You'll also have to shuffle some lines around, because you're creating frame2 on line 65. In order to be able to pass it to the constructor of Vostok you'll have to move line 65 before line 56.

You have another error of the same kind: the variable i is not visible in class TimerListener, because it's also a local variable in the main() method.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…And welcome to the Ranch
 
Rushan Tugushev
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ! Everything works !
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!