Results 1 to 13 of 13

Thread: MouseListener

  1. #1
    Aswq is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default MouseListener

    Hello Forum

    I am new in java.I am createing a Frame.Which have two label lb1,lb2.

    I want to set MouseListener for both label.How can i write plz tell me

    Thxs in advance..

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    First define the mouse listener as follows.

    Java Code:
                   MouseListener mouseListener = new MouseListener() {
                        public void mouseClicked(MouseEvent e) {
                        }
                        public void mouseEntered(MouseEvent e) {
                        }
                        public void mouseExited(MouseEvent e) {
                        }
                        public void mousePressed(MouseEvent e) {
                        }
                        public void mouseReleased(MouseEvent e) {
                        }
                    };
    Then simply add it to the label (any control).

    Did you try on the web, there you can found lots of examples. And also on the suns' web site you can find about How to Write a Mouse Listener

  3. #3
    Aswq is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default

    i try to add on this way but how can i add this for two label.I add some more even.Like ActionListener and it is working.

    Java Code:
     public void actionPerformed(ActionEvent e) { 
    	
    		 String actionCmd = e.getActionCommand();
    		 if (actionCmd.equals("Show File ")) {
    	            
    			 try {    
    
    						 File file = new File("");
    
    						
    							 
    							 String[] cmdArray = {"cmd", "/c", "start", "\"\"", file.getAbsolutePath()};
    							 Runtime.getRuntime().exec(cmdArray);
    
    
    
    					    }
    					   	catch (Exception x) 
    					   	{
    					    	x.printStackTrace();
    					   	
    					    }
    	        }
    
    
    		
    	         
    		 	else if (actionCmd.equals("Help Topics"))
    		 	{
    		 		try {    
    
    					 File file = new File("chm.chm");
    
    					
    						 
    						 String[] cmdArray = {"cmd", "/c", "start", "\"\"", file.getAbsolutePath()};
    						 Runtime.getRuntime().exec(cmdArray);
    
    
    
    				    }
    				   	catch (Exception x) 
    				   	{
    				    	x.printStackTrace();
    				   	
    				    }
    		 		
    		 		
    		 	}
    		
    	  
    	    }
    But how i MouseListener for like this.Becouse when i add like that
    Java Code:
     public void mouseClicked(MouseEvent e){
    		
    	      
    		if(e.getSource().equals(JLabel)
    		{
                                    // Some code here 
    
                                 }
    Then i am geting error in
    Java Code:
    if(e.getSource().equals(JLabel)
    Showing errorn on JLabel..

    How can i remove this problem..

    Plz help me

  4. #4
    Aswq is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default

    Sir i am useing MouseListener this way..
    Java Code:
    JLabel1.addMouseListener(this);
    JLabel2.addMouseListener(this);
    
    
     public void mouseClicked(MouseEvent e){
    		 
    		 if(e.getClickCount()==2)
    		 {
    	         try {    
    
    						 File file = new File("");
    
    						
    							 
    							 String[] cmdArray = {"cmd", "/c", "start", "\"\"", file.getAbsolutePath()};
    							 Runtime.getRuntime().exec(cmdArray);
    
    
    
    					    }
    					   	catch (Exception x) 
    					   	{
    					    	x.printStackTrace();
    					   	
    					    }	 }
    
    else
    {
    File file = new File("chm.chm");
    
    					
    						 
    						 String[] cmdArray = {"cmd", "/c", "start", "\"\"", file.getAbsolutePath()};
    						 Runtime.getRuntime().exec(cmdArray);
    
    }
    What happen both link is open click on any label.So How can i set the this sir.
    Plz help me

  5. #5
    Aswq is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default

    Hi All

    Plz help me any one..


    Thx's in Advance..

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    In simple way to solve this is implement two mouse listeners and add them to two labels. So you can implement different functionality there.

  7. #7
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Quote Originally Posted by Aswq View Post
    Hi All

    Plz help me any one..


    Thx's in Advance..
    Mr. Eranga is very help full man.So what he told you ,Just go through this..

    Any way this a code sample ,which may be help you..
    Java Code:
     public void mouseClicked(MouseEvent e){
    		 
    		 if(e.getSource() instanceof JLabel)
    		 {
    
    		 JLabel clicked = (JLabel)(e.getSource());
    
    		 if(clicked == one)
    		 {
                                }
                                 if(clicked==two)
                               {
                                 }

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Actually Mir, what your code doing is, get the click label event and depend on that do the processing. In that way you need only one mouse listener.

  9. #9
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    Actually Mir, what your code doing is, get the click label event and depend on that do the processing. In that way you need only one mouse listener.
    Sir you are right but i think [code]aswq[code] need mouse listener becouse ,he/she code is showing only mouse listener.

    Any way you are right Boss...

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  11. #11
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    There is two choices, he can you any he likes actually.
    Any way sir..i give ans first time...

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    That's better. You learn something here from the community, and the best thing is give something back our community. Well done pal. But always make sure that you gave the right answer. ;)

  13. #13
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    That's better. You learn something here from the community, and the best thing is give something back our community. Well done pal. But always make sure that you gave the right answer. ;)
    ok boss i will do that

Similar Threads

  1. Replies: 0
    Last Post: 04-23-2008, 08:26 PM
  2. Null array when passed to MouseListener
    By stevemcc in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 10:42 PM
  3. I need help with my MouseMotionAdapter and MouseListener.
    By MurderfaceX4 in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 03:13 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
  •