Results 1 to 3 of 3
  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default ActionListener in main

    lets say in my class App I have an instance variable of button ok. in the class constructor is instantiated and linked to a button("ok").

    Now in the main method, could I do App.ok.addActionListenernew ActionListener(){etc}???

    If so what is this called?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ActionListener in main

    Quote Originally Posted by stuckonjava View Post
    lets say in my class App I have an instance variable of button ok. in the class constructor is instantiated and linked to a button("ok").

    Now in the main method, could I do App.ok.addActionListenernew ActionListener(){etc}???

    If so what is this called?
    That's an anonymous inner class. And while this can be done, and I have no issue with using an anonymous inner class here, you have other problems with your example -- you would not want to expose your variables in this way, nor should they be static. Better that the ok variable not be public, and not be static. Better to give the App a public method that will allow you to do this with an App instance variable.
    Last edited by Fubarable; 05-15-2012 at 07:02 PM.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: ActionListener in main

    Could you provide a concrete example in the form of an SSCCE?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Exception in thread main java.lang.nosuchmethoderror main
    By prikesh_joshi7 in forum Java Applets
    Replies: 3
    Last Post: 01-26-2012, 04:19 PM
  2. Replies: 1
    Last Post: 11-23-2011, 05:52 AM
  3. Replies: 1
    Last Post: 01-23-2011, 05:19 PM
  4. Replies: 5
    Last Post: 10-06-2008, 12:11 AM
  5. Replies: 1
    Last Post: 08-06-2007, 09:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •