CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2007
    Posts
    40

    Question get mouse x and y coords from method new object listener call

    what's non trivila here is ,IMO that I would need to get the coordinates out of the following construction:

    Code:
    frame.addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent e) {e.getX(); e.getY();}
    		});
    the compilers allow only the following

    Code:
    final int a;
    final int b;
    frame.addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent e) {a=e.getX(); b=e.getY();}
    		});
    but then I cannot reference to them later as they are being errored out by the compiler as not initialized. If I try to assign values in the declaration, the compiler doesn't allow that either...

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: get mouse x and y coords from method new object listener call

    Can you explain what you trying to achieve and where you need to use the x, y co-ordinates?

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: get mouse x and y coords from method new object listener call

    If you declare a and b 'final' you can't assign values to them unless you do it in the declaration or in a constructor.

    To answer your question, we need to know the answer to Keang's question.

    All truths are easy to understand once they are discovered; the point is to discover them...
    G. Galilie
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Oct 2008
    Posts
    77

    Re: get mouse x and y coords from method new object listener call

    Quote Originally Posted by EVS View Post
    what's non trivila here is ,IMO that I would need to get the coordinates out of the following construction:

    Code:
    frame.addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent e) {e.getX(); e.getY();}
    		});
    the compilers allow only the following

    Code:
    final int a;
    final int b;
    frame.addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent e) {a=e.getX(); b=e.getY();}
    		});
    but then I cannot reference to them later as they are being errored out by the compiler as not initialized. If I try to assign values in the declaration, the compiler doesn't allow that either...
    Declaration of listener is in separate place from its implementation. You are trying to put both together.
    It goes like this:

    Code:
    //somewhere in constructor
    jPanelA.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jPanelAMouseClicked(evt);
                }
            });
    
    //somewhere in class
    private void jPanelAMouseClicked(java.awt.event.MouseEvent evt) {
            
         int x= evt.getX();
         int y= evt.getY();
    
        }
    If you decide to use NetBeans IDE, it will do this automatically for you.
    Last edited by postmortem; December 5th, 2008 at 01:49 AM.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: get mouse x and y coords from method new object listener call

    Quote Originally Posted by postmortem
    //somewhere in constructor
    jPanelA.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
    jPanelAMouseClicked(evt);
    }
    });

    //somewhere in class
    private void jPanelAMouseClicked(java.awt.event.MouseEvent evt) {

    int x= evt.getX();
    int y= evt.getY();

    }
    Err how does this help? The OP already has access to the x,y co-ordinates within the listener object's called method, extracting the code to another method makes no difference - it's just a different style.

  6. #6
    Join Date
    Feb 2008
    Posts
    966

    Re: get mouse x and y coords from method new object listener call

    Quote Originally Posted by dlorde View Post
    If you declare a and b 'final' you can't assign values to them unless you do it in the declaration or in a constructor.
    Or in a static block. It looks like he might be trying to do the following, but in a method.

    final int x;
    final int y;

    static
    {
    x = //mouse w/e
    y = //mouse w/e
    }
    Last edited by ProgramThis; December 5th, 2008 at 08:44 AM.

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: get mouse x and y coords from method new object listener call

    Quote Originally Posted by ProgramThis View Post
    Or in a static block.
    Yes - although he'd have to declare them static:
    Code:
    static final int x;
    static final int y;
    
    static
    {
        x = //mouse w/e
        y = //mouse w/e
    }
    And, of course, it can't be done in a method.

    If you lie to the compiler, it will get its revenge...
    Henry Spencer
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured