Results 1 to 5 of 5
Thread: want help on "THIS" KEY WORD
- 04-01-2011, 05:31 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
want help on "THIS" KEY WORD
HI FRIENDS I WANT UR HELP REGARDING "THIS" REFERNCE KEY WORD. i know its use to refer present object but i am getting little confuse.
can anyone tell me any way/trick/ how to identifiy to which object "this" keyword refernces .
i am positing 2 codes please tell whether i am wrong or write
first code
second code does keyword "this" in below code refer to object "button1" or "mtextbtn..n"Java Code:import java.awt.*; import java.awt.event.*; import java.applet.*; public class keyevent1 extends Applet implements KeyListener { public void init() { requestFocus(); // to request focus on applet addKeyListener(this);[B][COLOR="Red"]does "this " refernce to applet[/COLOR][/B] } public void keyPressed(KeyEvent tt) { switch(tt.getKeyChar()) { case ‘r’: case ‘R’: setBackground(Color.red);break; case ‘g’: case ‘G’: setBackground(Color.green);break; case ‘b’: case ‘B’: setBackground(Color.blue);break; case ‘y’: case ‘Y’: setBackground(Color.yellow);break; default: setBackground(Color.black); } } public void keyReleased(KeyEvent tt) { } public void keyTyped(KeyEvent tt) { } }
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Button1 extends JFrame implements ActionListener { JButton mtextbtn1; JButton mtextbtn2; JButton mtextbtn3; JButton mtextbtn4; JButton mtextbtn5; public Button1() { setTitle("BUTTON EXAMPLE"); JPanel contentpane = (JPanel)getContentPane(); contentpane.setLayout(new FlowLayout()); //contentpane.setLayout(new GridLayout(2,3)); mtextbtn1 = new JButton("Enabled"); mtextbtn1.setMnemonic('E'); mtextbtn1.addActionListener(this); [COLOR="Red"]this refers to whom button1 or mtexbtn1[/COLOR] contentpane.add(mtextbtn1); mtextbtn2 = new JButton("Disabled"); mtextbtn2.setMnemonic('D'); mtextbtn2.addActionListener(this); contentpane.add(mtextbtn2);[COLOR="Red"]this refers to whom button1 or mtexbtn2[/COLOR] mtextbtn3 = new JButton("Add"); mtextbtn3.setMnemonic('A'); mtextbtn3.addActionListener(this);[COLOR="Red"]this refers to whom button1 or mtexbtn4[/COLOR] contentpane.add(mtextbtn3); mtextbtn4 = new JButton("Fourth"); mtextbtn4.setMnemonic('F'); mtextbtn4.addActionListener(this);[COLOR="Red"]this refers to whom button1 or mtexbtn4[/COLOR] contentpane.add(mtextbtn4); mtextbtn5 = new JButton("Fifth"); mtextbtn5.setMnemonic('T'); mtextbtn5.addActionListener(this);[COLOR="Red"]this refers to whom button1 or mtexbtn5[/COLOR] contentpane.add(mtextbtn5); mtextbtn1.setEnabled(true); myadapter myapp = new myadapter(); addWindowListener(myapp); } class myadapter extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } public void actionPerformed(ActionEvent e) { if(e.getSource() == mtextbtn1) { setTitle("First Button Clicked"); } else if (e.getSource() == mtextbtn2) { setTitle("Second Button Clicked"); } else if (e.getSource() == mtextbtn3) { setTitle("Add Button Clicked"); } else if (e.getSource() == mtextbtn4) { setTitle("Fourth Button Clicked"); } else if (e.getSource() == mtextbtn5) { setTitle("Fifth Button Clicked"); } } public static void main(String args[]) { Button1 b = new Button1(); b.setSize(100,100); b.setVisible(true); } }
- 04-01-2011, 05:41 PM #2
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
In first code: this refers to object of keyevent1 class.
In second code: this refers to object of Button1 class.
The best way to identify this type is:
Java Code:SySystem.out.println(this.getClass());
-
I think you've confused this for referring to Objects like in JavaScript.
as lovelesh has explained, this refers to the current Class, not the object container.
- 04-02-2011, 11:02 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
Last edited by anoop_mig25; 04-02-2011 at 11:05 AM.
-
There's not much to it, here's a simple tutorial about the this keyword:
Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
As for your program, this is how you dynamically add Actions to buttons:
Java Code://declare buttons private JButton mybutton0 = new JButton(); private JButton mybutton1 = new JButton(); private JButton mybutton2 = new JButton(); //add to list so we can loop through each button java.util.List<JButton> mybuttons = new java.util.ArrayList<JButton>(3); mybuttons.add(mybutton0); mybuttons.add(mybutton1); mybuttons.add(mybutton2); //create a loop to add button actions for (int i=0; i<mybuttons.size(); i++) { //adding the button action mybuttons.get(i).addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //external method mybuttonsAction(i); } } ); }
Then somewhere else in your class have this method:
Java Code:public void mybuttonsAction(int actionNo) { switch(actionNo) { case 0: //commands for button0 break; case 1: //commands for button1 break; case 2: //commands for button2 break; default: //unrecognized actionNo break; } }
Similar Threads
-
Reserved word "throws"
By Lil_Aziz1 in forum New To JavaReplies: 5Last Post: 01-02-2010, 02:12 PM -
Count lines cointaining "word" in input file
By gwithey in forum New To JavaReplies: 5Last Post: 04-02-2009, 05:23 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
replacing last comma in a string with the word "or"
By wprjr in forum New To JavaReplies: 1Last Post: 05-07-2008, 01:19 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks