File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Difficulties with ActionListeners and non-static methods. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Difficulties with ActionListeners and non-static methods. " Watch "Difficulties with ActionListeners and non-static methods. " New topic
Author

Difficulties with ActionListeners and non-static methods.

Kevin Corina
Greenhorn

Joined: Nov 29, 2012
Posts: 14
Ok, Ive been learning java for awhile now, I primarily use "Head First Java" , and I'm self taught. I've written a program called "counter". All counter does is make a JPanel with JButtons and a line of text. Each button when clicked either adds or subtracts from the value in the text field. It is terribly written. Like, seriously terrible. So I decided to properly encapsulate it. Here is the code for the original Counter. RealFirestarter is executed. and Counter runs:



RealFirstarter

All RealFirestarter does is create a new Counter named "A"


Counter



Here is the code for the second program.

Firestarter



GraphicBox



and finally Counter



What I need to understand is how I can have a non-static counter of interact with static methods inside of inner classes "UpListener" and "DownListener" Is there a method similar to "AddActionListener", but non-static? That way I could make two new actionListeners and THEN apply them to a JButton? Obviously second the program isn't finished, it still need to be fixed to update the text field when the buttons are clicked, I want to get through this bug first.

So, yeah. Thats really it. Thanks guys! And please remember, I relatively new to this.

EDIT* took out some unnecessary spaces, and code. Sorry, couldn't really take much out of the code, its all (as far as I can tell) relevant to my issue.
EDIT** updated the "addActionListener" methods in the second Firestarter.
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
Welcome to Ranch Kevin !!!

I think its too much of code you have posted. Try to post an SSCCE (<-- click)
Kevin Corina
Greenhorn

Joined: Nov 29, 2012
Posts: 14
Ok, Ill see what I can do. Have any suggestions though on what I can do to fix the problem?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Kevin Corina wrote:What I need to understand is how I can have a non-static counter of interact with static methods inside of inner classes "UpListener" and "DownListener" Is there a method similar to "AddActionListener", but non-static? That way I could make two new actionListeners and THEN apply them to a JButton?

I am not sure what your question is, but JButton#addActionListener() is not static. From what I can see, the one method inside UpListener and DownListener is the non-static method actionPerformed(). And when you do Up.addActionListener(new UpListener()); you are, indeed, making a new (see the new keyword) instance of an ActionListener and then applying it to the JButton (instance).

I think that SSCCE is needed, and you need to explain in more detail the issue you are having, because what you said doesn't match the code or how JButton or ActionListener (or an interface for that matter) works.


Steve
Kevin Corina
Greenhorn

Joined: Nov 29, 2012
Posts: 14
Steve Luke wrote:
Kevin Corina wrote:What I need to understand is how I can have a non-static counter of interact with static methods inside of inner classes "UpListener" and "DownListener" Is there a method similar to "AddActionListener", but non-static? That way I could make two new actionListeners and THEN apply them to a JButton?

I am not sure what your question is, but JButton#addActionListener() is not static. From what I can see, the one method inside UpListener and DownListener is the non-static method actionPerformed(). I think that SSCCE is needed, and you need to explain in more detail the issue you are having, because what you said doesn't match the code or how JButton or ActionListener (or an interface for that matter) works.


wait, Im sorry this I posted the wrong code. let me repost.

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Also: regarding this: Up.CreateActionListener(new UpListener()); in the Firestarter code. A JButton does not have a method named CreateActionListener(). It has a method named createActionListener() (difference is the captial C versus lower case c, Java is case-sensitive). But that method doesn't take an ActionListener as a parameter (is has an empty parameter list) and is probably not accessible by your code. So, not sure what you are trying to do there but whatever it is, it won't work.
Kevin Corina
Greenhorn

Joined: Nov 29, 2012
Posts: 14
Steve Luke wrote:Also: regarding this: Up.CreateActionListener(new UpListener()); in the Firestarter code. A JButton does not have a method named CreateActionListener(). It has a method named createActionListener() (difference is the captial C versus lower case c, Java is case-sensitive). But that method doesn't take an ActionListener as a parameter (is has an empty parameter list) and is probably not accessible by your code. So, not sure what you are trying to do there but whatever it is, it won't work.


Ok, I thought I had returned my code to what it originally was, but I seem to have broken it further. I really appreciate your patience. Ill get this fixed soon.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Kevin Corina wrote:Ok, I thought I had returned my code to what it originally was, but I seem to have broken it further. I really appreciate your patience. Ill get this fixed soon.


Maybe stop worrying about that code for a minute. Speak about your problem in words. What is it you are trying to do. What is the stumbling block. Then, when it is time for code try some code with:
1) One class that contains a JFrame and a single JButton with an ActionListener
2) Another class with a single method which does a System.out.println()

And use that code as an example of the problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
How many frame objects do you have? Remember a frame is a top level container and you should have exactly one top level container per application.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
Moved to our GUIs forum.
Kevin Corina
Greenhorn

Joined: Nov 29, 2012
Posts: 14
Ok, here we go. Sorry about the delay, been kind of busy lately.

So I made Simple version of the program, to encounter a much smaller, more reasonable set of compilation errors.
I have refrained from posting the code for the Counter Class because isn't being used.

Firestarter



GraphicsBox



When I try to compile it, I get these errors:
SimpleGraphicsBox.java:16: error: constructor ButtonListener in class SimpleGraphicsBox.ButtonListener cannot be applied to given types;
Up.addActionListener(new ButtonListener(A));
^
required: no arguments
found: SimpleCounter
reason: actual and formal argument lists differ in length
SimpleGraphicsBox.java:23: error: SimpleGraphicsBox.ButtonListener is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
class ButtonListener implements ActionListener {
^
2 errors
I think its bugging out over the fact I overridden the actionPreformed method from GraphicsBox.
Yeah. Any ideas?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

Kevin Corina wrote:I think its bugging out over the fact I overridden the actionPreformed method from GraphicsBox.


What method was that again? No, you didn't override the method you mentioned there. That's because there is no such method to override. There is however another method with a very similar name which you should have overridden, but didn't. Look more carefully at the error message and the line of code you think is right but you know is wrong.
Kevin Corina
Greenhorn

Joined: Nov 29, 2012
Posts: 14
Paul Clapham wrote:
Kevin Corina wrote:I think its bugging out over the fact I overridden the actionPreformed method from GraphicsBox.


What method was that again? No, you didn't override the method you mentioned there. That's because there is no such method to override. There is however another method with a very similar name which you should have overridden, but didn't. Look more carefully at the error message and the line of code you think is right but you know is wrong.


well, that was slightly cryptic, but Ill check it out.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

You want less cryptic? Check your spelling.
Kevin Corina
Greenhorn

Joined: Nov 29, 2012
Posts: 14
Paul Clapham wrote:You want less cryptic? Check your spelling.


Yeah... I got that XD



So I realized that I was spelling "Performed" as "Preformed" which solved one of my problems.

now what I'm trying to override the Constructor to take a SimpleCounter. Now my real problem is storing it for use in other methods. I can take the SimpleCounter , I can seemingly store it in a object reference, but I cant seem execute increaseValue or returnValue in ActionPerformed.

Here is my ButtonListener.


Thanks for your help so far by the way!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4492
    
    8

You've declared your C variable as a local variable within the constructor. You need to declare it as an instance variable if you want to be able to access it in other methods.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2420
    
  50
And, whilst this doesn't affect the operation of your code, you've called your variable 'C'. Two things: variables in Java, by convention, start with a lower case letter and you should always use meaningful names for variables. You may know what 'C' is as you've just written the code but in 6 months time you probably won't and more importantly it makes it hard for other people to read and understand your code.
Kevin Corina
Greenhorn

Joined: Nov 29, 2012
Posts: 14
Matthew Brown wrote:You've declared your C variable as a local variable within the constructor. You need to declare it as an instance variable if you want to be able to access it in other methods.


OOHHHHH.

Annddd... that did it! Thanks guys! I might come back to this thread later if I encounter something else as I finish my program, but this has been a huge help!

Kevin Corina
Greenhorn

Joined: Nov 29, 2012
Posts: 14
Tony Docherty wrote:And, whilst this doesn't affect the operation of your code, you've called your variable 'C'. Two things: variables in Java, by convention, start with a lower case letter and you should always use meaningful names for variables. You may know what 'C' is as you've just written the code but in 6 months time you probably won't and more importantly it makes it hard for other people to read and understand your code.


Good idea, I'll try it. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difficulties with ActionListeners and non-static methods.