1

I'm implementing my own drag-and-drop functionality, and I've reduced my problem to this:

The code below works and will invoke mousePressed() and mouseReleased() as needed.
However, if you uncomment frame.add(label), mouseReleased() is never called.

JFrame frame = new JFrame();
frame.setSize(new Dimension(100, 100));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label = new JLabel("Click Me!");
label.addMouseListener(new MouseAdapter() 
{
    public void mousePressed(MouseEvent e) 
    {
        System.out.println("Pressed");
        //frame.add(label);
    {

    public void mouseReleased(MouseEvent e) 
    {
        System.out.println("Released");
    }
});

frame.add(label);
frame.setVisible(true);

Why is mouseReleased() never called after the label is re-added to the frame's panel?

Is there a way to work around this problem?

0 Answers0