GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Aug-2006, 06:49
_Y_ _Y_ is offline
New Member
 
Join Date: Aug 2006
Posts: 11
_Y_ is on a distinguished road

addActionListener(this) conflicts with static


I have started to recall Java and encountered the following problem. Help, pls.

A simple window with a button. So far as the program has to react to the button click, I describe the class as
public class Class1 implements ActionListener {...

The program should run on a local computer, i.e. it is not an applet. Thus the execution must (IMHO) begin from
public static void main(String[] args) {...

To react on the button click, I must (IMHO) write
button1.addActionListener(this);

However, it gives an error. The message is "Cannot use this in a static context". I guess that this points to the instance of an object but not to the class, while there is no class (because of static). Cannot get how to solve this problem.

Thank you!
  #2  
Old 02-Aug-2006, 10:16
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 966
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: addActionListener(this) conflicts with static


a button needs an ActionListener to perform action for the button. Check out the code here, see: private class ButtonListener, for example code.
http://www.gidforums.com/showpost.ph...93&postcount=1

Without seeing more of what you are trying to do, this is all I can suggest. There is a way to use 'this', but I'd have to see what you have done. What it SOUNDS like, is that you are trying to use addActionListener() WITHIN main(), which is static in context. I don't know why so many Java users try to do it that way, for it's not using the OO capability of Java.

1. Create your button and modify its properties in a constructor.
2. main() should theoretically only have one statement that creates an instance of the class, something like this: (this is skeleton code, with your class name as an example)
JAVA Code:
public class Class1 implements ActionListener {
    // variable(s) here

    public static void main(String[] args) {
         Class1 c1 = new Class1();
    }

    public Class1()
    {
         // intialize variables and other objects here...
         // setting up any GUI could be done here too...
         ...
         button1.addActionListener(this);  // should work here.
         ...
    }

    // any other function member(s) the class may need for support
}
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
Last edited by TurboPT : 02-Aug-2006 at 11:43.
  #3  
Old 03-Aug-2006, 01:47
_Y_ _Y_ is offline
New Member
 
Join Date: Aug 2006
Posts: 11
_Y_ is on a distinguished road

Re: addActionListener(this) conflicts with static


Thank you

I actually tried a similar idea before asking here but I located
public static void main(String[] args) {...
in a separate object and thus in a separate file. So, I got "Fatal exception" without any explanation and concluded that the idea is wrong It works now.
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
calling static member function through object alcoholic C++ Forum 3 06-Dec-2005 06:10
Bloodshed Dev C++ Project Options JdS C++ Forum 6 11-Nov-2005 17:23
error related to static member functions and varaibles kai85 C++ Forum 3 20-May-2005 06:11
Convert Java to C++??? leanieleanz C++ Forum 1 04-Mar-2005 19:06
changing caption of static text control Pandiani MS Visual C++ / MFC Forum 1 20-Sep-2004 01:38

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.