简体   繁体   中英

MouseListener event MouseExited doesn't work on undecorated dialog over disabled frame

i've got some trouble with the mouseExited event. I have one undecorated JDialog with a MouseListener, this JDialog is half over one disabled JFrame. The mouseExited event is fired when the mouse exit dialog and go on the desktop, but if the mouse exit dialog and go over the disabled frame the event isn't fired. This happens only if the frame is disabled. And i don't know why.. Someone can help me?

Here is a fast example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MouseListenerTest {
    public static void main(String a[]) {
        System.out.println("java.version: " + System.getProperty("java.version"));
        JFrame ownerFrame = new JFrame("Hello i am the owner frame :)");
        ownerFrame.setBounds(100,100,500,500);
        ownerFrame.setVisible(true);
        ownerFrame.setEnabled(false);
        JDialog topDialog = new JDialog(ownerFrame, "Hello i am the top dialog");
        topDialog.getContentPane().setBackground(Color.YELLOW);
        topDialog.setUndecorated(true);
        final JLabel xLabel = new JLabel("I am OUT");
        xLabel.setHorizontalAlignment(JLabel.CENTER);
        topDialog.getContentPane().add(xLabel, BorderLayout.CENTER);
        topDialog.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseEntered(MouseEvent e) {
                System.out.println("I am IN");
                xLabel.setText("I am IN");
            }

            @Override
            public void mouseExited(MouseEvent e) {
                System.out.println("I am OUT");
                xLabel.setText("I am OUT");
            }});
        topDialog.setBounds(500,200,200,200);
        topDialog.setVisible(true);
    }
}

Component#setEnabled(boolean) (Java Platform SE 8 )
Note: Disabling a heavyweight container prevents all components in this container from receiving any input events. But disabling a lightweight container affects only this container.

JFrame is a heavyweight (top level) component, so I think that this behavior is specification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM