• Post Reply Bookmark Topic Watch Topic
  • New Topic

the keyword this using it with ActionListener  RSS feed

 
pritesh mangi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the equivalent way of using this keyword for this line " listener = new TestListener(this); ", i don't understand the keyword using with following line; can anyone please provide equivalent way of doing it.



 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You weren't quite right with the code tags, but I have corrected that.

There is something not quite right about you linking your test frame class with that listener class. You are using a non‑private field, so you have tight coupling between those two classes. There is also something not right about using the action commands.
Also you have a non‑private field command which is not initialised in the constructor, so you can call the runButtonEvent method and suffer a NullPointerException.
If you want a Listener which changes the colour of a Component, I have my own ideas how you write it. Try here. The fields are initialised in the constructor so you might add a requirement they be not null. The Component changes colour as requested. You can even add a setColour method. Then you can change the redButton action to turn the component yellow
 
pritesh mangi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you answering my question but I wanted to know what is the another way of doing it instead of using "this" keyword for this line:

listener = new TestListener(this);

so instead of using the this keyword in that line what is the another way of doing it, I know that this is used for referring to the current object that class but i want to know another way, without using "this" keyword.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean something like this?If you use this, you are changing the colour of the object you happen to be in, which works as long as it is an instance of JComponent or whichever type you require for the listener's constructor.
 
pritesh mangi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, you know when you use the keyword "this" for the following line; listener = new TestListener(this); , in this case the keyword "this" refers to the TestFrame class's object which executes the code, so if i don't want to use the keyword "this", what can i pass in that bracket, instead of "this" keyword.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any identifier which is a reference at any object of the type taken by the constructor parameter, or
new TestFrame(...)
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And this is where you see your poor design for the Listener class making its presence felt.
 
pritesh mangi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, thanks
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!