Results 1 to 3 of 3
  1. #1
    SM2010 is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default pointers needed please (re: MouseListener)

    Hi folks - I've just started learning java and am just trying to put a simple test together with a frame, a label, and a MouseListener on the label, which will tell me when it's been clicked.

    Anyway, at first I didn't have a constructor (for no particular reason, was just seeing if it would work without one) just a main method and the override ML methods, and I couldn't add the ML to the label or the frame, it just complained that I was trying to add a non-static variable to a static method (I think).

    Eventually I added a constructor and it seems to be okay with adding the ML in there.

    But now when I try one of the events, ie. click, I can't reference the label that I want to change from inside the mouseClicked() method.

    I'd be v.grateful if anyone could give me some advice on my points below, I haven't quite got the grasp of oo programming just yet, and am getting a bit tied up in knots with scope, events, etc. :(

    -how should I reference the label from inside the ML methods?
    -in laymans terms, why couldn't I use the constructor code in main()?
    -I don't quite understand what I need to use (this) in adding the ML to the label, I was expecting it to work with l.addML(). What does the (this) parameter actually do here?

    Many thanks for your help! :)

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class JFrameTester implements MouseListener {
    
      public JFrameTester() {
    	JFrame f = new JFrame("JFrame");
    	JLabel l = new JLabel("label");
    
        f.getContentPane().add(l);
    
    	l.addMouseListener(this);
    
        f.setVisible(true);
    }
    
      public static void main(String[] args) {
        JFrameTester j = new JFrameTester();
      }
    
    public void mouseExited(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    
    public void mouseClicked(MouseEvent e) {
    	//l.setText("clicked");
    }
      
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your JLabel is declared within the constructor is thus only visible from within the constructor, and this is why th mouseClicked method can't "see" it. The solution is to declare the JLabel in the class, not the constructor. Sure use it in the constructor but just don't declare it there.

    As an aside, similar to you, I tried to do Swing coding when first learning Java but found the going very difficult and almost quit Java. I then put Swing aside and later tried it again after I had gone through a basic Java textbook and the Sun tutorials and then found the going much easier after obtaining basic Java knowledge. You may note the same benefit by first leaning Java basics before trying to climb the steep hill of GUI coding.

    Much luck!

  3. #3
    thomasdevos's Avatar
    thomasdevos is offline Member
    Join Date
    Feb 2010
    Posts
    19
    Rep Power
    0

    Default

    hi, SM2010
    the reason you do not get what you want is the method mouseClicked can not access your JLabel object l.
    Anonymous class is good solution for you.
    i just make some twist on your code, you can find out how it works there.
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class JFrameTester {
    
    	public JFrameTester() {
    		JFrame f = new JFrame("JFrame");
    		final JLabel l = new JLabel("label");
    
    		f.getContentPane().add(l);
    
    		l.addMouseListener(new MouseListener() {
    
    			public void mouseClicked(MouseEvent e) {
    				// TODO Auto-generated method stub
    				 l.setText("clicked");
    			}
    
    			public void mouseEntered(MouseEvent e) {
    				// TODO Auto-generated method stub
    
    			}
    
    			public void mouseExited(MouseEvent e) {
    				// TODO Auto-generated method stub
    
    			}
    
    			public void mousePressed(MouseEvent e) {
    				// TODO Auto-generated method stub
    
    			}
    
    			public void mouseReleased(MouseEvent e) {
    				// TODO Auto-generated method stub
    
    			}
    
    		});
    
    		f.setVisible(true);
    	}
    
    	public static void main(String[] args) {
    		JFrameTester j = new JFrameTester();
    	}
    
    }
    Fubarable, i agree with you. the hardest part of java is beginning. time is the only cure.


    thomas devos
    i hold 7 years develop exp. now i start a thread to share my knowlege about a j2ee project. welcome to participate.Study Java Through Real Java Project

Similar Threads

  1. object new and pointers error
    By hydride in forum New To Java
    Replies: 2
    Last Post: 04-06-2010, 06:15 PM
  2. Does Java support pointers?
    By kthaker in forum New To Java
    Replies: 2
    Last Post: 10-07-2009, 08:16 AM
  3. pointers and wrapper classes
    By becky in forum New To Java
    Replies: 11
    Last Post: 02-07-2009, 03:59 AM
  4. Java pointers? How to...
    By Krooger in forum New To Java
    Replies: 4
    Last Post: 11-04-2008, 08:30 PM
  5. Pointers
    By ravian in forum New To Java
    Replies: 5
    Last Post: 11-28-2007, 01:49 PM

Posting Permissions

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