Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

odd mouselistener issue

Thread view: 
Peter Ashford - 04 Mar 2006 02:14 GMT
Hi Guys

I'm probably doing something stupid... but:

I've got a JFrame with borderlayout.  In the centre box, I've got a
JPanel.  If I add a mouselistener to the jframe, it works fine (but
I've got coords relative to the frame which is correct but not what I
want - I want coords relative to the panel). However, when I add the
mouselistener to the panel, I get no mouseevents being returned at all.

Any ideas in what way I might be being stupid?

TIA

Peter.
Knute Johnson - 04 Mar 2006 04:17 GMT
> Hi Guys
>
[quoted text clipped - 11 lines]
>
> Peter.

It is always easier for somebody to diagnose your problem if you include
your code.  So all I can do is give you an example of how to add a
MouseListener to a JPanel.  Don't forget that BorderLayout will cause
your component to expand to the size of your container.  I usually only
use BorderLayout when I have one component to add to the container.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test {
    public static void createGUI() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new JPanel();
        p.setBackground(Color.BLUE);
        p.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me) {
                System.out.println(me.getX()+" "+me.getY());
            }
        });
        f.add(p,BorderLayout.CENTER);
        f.setSize(640,480);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                createGUI();
            }
        };
        EventQueue.invokeLater(r);
    }
}

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.