• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Trouble JButton + Superclass Method

 
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.
 
Saloon Keeper
Posts: 10308
217
  • 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!
 
Marshal
Posts: 64686
225
  • 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: 10308
217
  • 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
Marshal
Posts: 64686
225
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!