Discussion:
getting mouse click events of a JPanel
(too old to reply)
Allan Bruce
2003-12-18 14:36:10 UTC
Permalink
I want to be able to detect when a mouse button is clicked in my JPanel.
The code below seems to only work for the forw where no components are. How
do I change this so that I can get the events for the JPanel? Also, is
there a way to determine at what co-ordinates the JPanel is at, i.e. the
upper-left and the lower-right ?
Thanks
Allan

// in constructor
addMouseListener(this);

// elsewhere
public void mouseClicked(MouseEvent e)
{
if (e.getButton() == e.BUTTON1) // left mouse click
{
System.out.print("Left ");
}
else
capturing = false;
System.out.print("Button Clicked\n");
}
Chris Smith
2003-12-18 20:11:46 UTC
Permalink
Post by Allan Bruce
I want to be able to detect when a mouse button is clicked in my JPanel.
The code below seems to only work for the forw where no components are. How
do I change this so that I can get the events for the JPanel? Also, is
there a way to determine at what co-ordinates the JPanel is at, i.e. the
upper-left and the lower-right ?
To get notifications of all such clicks, you'd need to add the
MouseListener to all of the components in that tree. Alternatively, you
can temporarily intercept all mouse clicks in an area by using a
JRootPane and making the glass pane visible.

Further comment would require knowing what you want to accomplish.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Continue reading on narkive:
Loading...