0

I have a JFrame with a JPanel of FlowLayout. A JButton is placaed on the panel. I want the JFrame to be closed whenever mouse is moved out of the JFrame. It works fine.

But it does the same even when the mouse moves onto the JButton!! And I dont want that!

Code of JFrame Class, MyFrame.java:

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

public class MyFrame extends JFrame {

    JButton btnOne;     JPanel panOne;

    public MyFrame() {}
    public MyFrame() {
        this.setSize(400, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.initComps();
        this.setFrameMouseActions();
        this.setVisible(true);
    }

    public void initComps() {
        panOne = new JPanel();
        btnOne = new JButton("Click Me!");
        panOne.add(btnOne);
        this.add(panOne);
    }

    public void setFrameMouseActions() {
        this.addMouseListener(new MouseListener() {
            public void mouseExited(MouseEvent e) {
                dispose();
                JOptionPane.showMessageDialog(null, "Disposed", "Disposed", JOptionPane.INFORMATION_MESSAGE);
                System.exit(0);
            }
            public void mouseReleased(MouseEvent e) {}
            public void mousePressed(MouseEvent e) {}
            public void mouseEntered(MouseEvent e) {}
            public void mouseClicked(MouseEvent e) {}
        });
    }
}

Code of Main Class, MouseMain.java:

public class MouseMain {            
    public static void main(String[] args) {
        MyFrame frameOb = new MyFrame();
    }
}

NOTE: I have tried with null layout too(although its bad practice!). But its all the same. And this code is a simplified version from a large project of mine.

Minar Mahmud
  • 2,577
  • 6
  • 20
  • 32
  • 2
    The problem is, mouseExited fires when ever the mouse moves out of event range (it's no longer possible for the component to monitor mouse events) of the component. There is no "real" way around it. You could, howver, use the MouseInfo class with a javax.swing.Timer and check to see if the mouse is still within th bounds of the frame, but this is a hack. – MadProgrammer Feb 03 '15 at 08:46
  • 1
    *"I want the JFrame to be closed whenever mouse is moved out of the JFrame"* That sounds like an awful, horrid GUI. How about you ***don't*** do that? – Andrew Thompson Feb 03 '15 at 14:36
  • You probably have missed the last line of the ***NOTE*** section. – Minar Mahmud Feb 03 '15 at 14:38
  • @MadProgrammer thanks for the info. Going to try it. :) – Minar Mahmud Feb 03 '15 at 16:30

1 Answers1

1

// Phew. It is too hard. I find out barely.

The main point is differences of both method. Click link first. And see this code.

frame.getContentPane().getSize() , frame.getWidth()

package test;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

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

public class TestCursorOut extends JFrame {

    private JPanel contentPane;
    private static TestCursorOut frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frame = new TestCursorOut();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestCursorOut() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(200, 150, 450, 300);
        contentPane = new JPanel();
        contentPane.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {

                if(!(e.getX() <frame.getContentPane().getWidth() && e.getX() > 0))
                {
                    ExitCursorMethod();
                }

                if(!(e.getY() <frame.getContentPane().getHeight() && e.getY() > 0))
                {
                    ExitCursorMethod();
                }
            }

            public void ExitCursorMethod() {
                dispose();
                JOptionPane.showMessageDialog(null, "Disposed", "Disposed", JOptionPane.INFORMATION_MESSAGE);
                System.exit(0);
            }
        });
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {

            }
        });
        btnNewButton.setBounds(160, 10, 97, 23);
        contentPane.add(btnNewButton);
    }

}
Community
  • 1
  • 1
David Kim
  • 122
  • 2
  • 2
  • 11