Results 1 to 5 of 5
  1. #1
    anoop_mig25 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default 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

    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) { }
    }
    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 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);
    			}
    }

  2. #2
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    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());

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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.

  4. #4
    anoop_mig25 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by lovelesh View Post
    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());
    Quote Originally Posted by ozzyman View Post
    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.

    thanks both of u for reply but i didn`t get first one . can u explain me a lot dipper although i think i got second one write but it may be an hook so can u please provide me link for tutorial/video link were i can clear my concepts
    Last edited by anoop_mig25; 04-02-2011 at 12:05 PM.

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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

  1. Reserved word "throws"
    By Lil_Aziz1 in forum New To Java
    Replies: 5
    Last Post: 01-02-2010, 03:12 PM
  2. Count lines cointaining "word" in input file
    By gwithey in forum New To Java
    Replies: 5
    Last Post: 04-02-2009, 06:23 AM
  3. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  5. Replies: 1
    Last Post: 05-07-2008, 02:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •