Re: Fire mouseExited only when leaving panel boundaries?
- From: Vova Reznik <address@xxxxxxxx>
- Date: Wed, 08 Feb 2006 22:09:29 GMT
PilotYid wrote:
I am having some trouble with the mouseExited event in
MouseAdapter/MouseListener. I have a Panel with 3 labels on it. I would
like to start a timer when the mouse is anywhere over this panel
(including the labels), and stop the timer when it leaves the panel
boundaries. I have added a mouselistener to do this but when moving
internally within the panel from the panel to a label, I am getting a
mouseexited on the Panel which is stopping the timer. Is there anyway
to get around this? I only want to do something upon leaving the Panel
boundaries, not when moving internally within the panel to a label,
etc.
The labels inside the Panel do not have to handle any events.
I have to use Java 1.1 as well. Would some kind of glasspane work here?
Thanks for your help,
Aaron
It is much easier for me to write code than write on English :):)
(I had several notice about bad spelling and etc)
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class TransferMouseEvent extends JFrame implements MouseListener, ActionListener {
private Timer timer ;
TransferMouseEvent() {
getContentPane().addMouseListener(this);
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 20, 30));
JLabel lbl = new JLabel("Label");
lbl.setOpaque(true);
lbl.setBackground(Color.BLUE);
getContentPane().add(lbl);
getContentPane().add(new JButton("Button"));
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
timer = new Timer(500, this);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new TransferMouseEvent().setVisible(true);
}
});
}
/**
*
*/
private static final long serialVersionUID = 1L;
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
if(!isCoveredByChildComp(getContentPane(), e.getPoint())){
timer.start();
}
}
public void mouseExited(MouseEvent e) {
Point p = e.getPoint();
//System.out.println("Exited to cover child? " + isCoveredByChildComp(getContentPane(), p));
if(!isCoveredByChildComp(getContentPane(), p)){
timer.stop();
}
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
private boolean isCoveredByChildComp(Container parent, Point p){
Component []comp = parent.getComponents();
for(int i=0; i<comp.length; i++){
if(comp[i].getBounds().contains(p)){
return true;
}
}
return false;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == timer){
System.out.println("Tick-tack");
}
}
}
Another problem - you may need to go deeper - for example
your panel may contain another Container with another Component(s) or Container(s). Then isCoveredByChildComp may be recursive!!!
.
- References:
- Fire mouseExited only when leaving panel boundaries?
- From: PilotYid
- Fire mouseExited only when leaving panel boundaries?
- Prev by Date: Re: Fire mouseExited only when leaving panel boundaries?
- Next by Date: Re: setCursor() not working on disabled JEditorPane
- Previous by thread: Re: Fire mouseExited only when leaving panel boundaries?
- Next by thread: Re: adding MenuBar in AWT - crashes free VMs
- Index(es):
Relevant Pages
|