• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble JButton + Superclass Method  RSS feed

 
Gabriel Gasparini
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble calling some of my methods of my superclass inside my JButton ActionListener. What I'm trying to do is something like this:




When I call my method in the ActionListener I get an error. Can I do this ? I would like to call a method from my superclass when the button is clicked !

Ty.
 
Stephan van Hulst
Saloon Keeper
Posts: 6981
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class inherits from Superclass, you can just call method() from actionPerformed(). You can not use the this or super keywords, since they would refer to the ActionListener instance and the ActionListener parent respectively. Instead, you should use Class.this and Class.super.

Welcome to CodeRanch, Gabriel!
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again. Please don't call your class “Class” because there already is a class called “Class”.

I added code tags which you should always use to your post. Doesn't it look better now
 
Gabriel Gasparini
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Class inherits from Superclass, you can just call method() from actionPerformed(). You can not use the this or super keywords, since they would refer to the ActionListener instance and the ActionListener parent respectively. Instead, you should use Class.this and Class.super.

Welcome to CodeRanch, Gabriel!


Oh, tank you... now it worked ! I removed the super. and called the method alone, and it works.

But... whats the diference between using Class.method(); or just calling method(); ?

Ty for the help.... and sorry for my bad english, im from Brazil. =)
 
Gabriel Gasparini
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome again. Please don't call your class “Class” because there already is a class called “Class”.

I added code tags which you should always use to your post. Doesn't it look better now


The name Class was just an example... im my code the Class has another name... kkkkkkk.

With the codetags the code looks much better.... how do I use it ? Tanks for the help.
 
Stephan van Hulst
Saloon Keeper
Posts: 6981
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class.method() would call a static method of a class named Class (which Campbell already explained is a bad way to name your classes).

Class.this.method() would call a method on the current instance of a class named Class. Class.this is useful if you need to refer to the containing class of an inner class (like an anonymous class).
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are good reasons for always copying and pasting code so it is exactly what you are using; you can get subtle errors from that class Class and not know why.
The text saying “code tags” is a link: follow it, and also look at this link which Tim Cooke recently updated.
 
Gabriel Gasparini
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ty all for the help. I solved my problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!