1

I asked a fuzzy question hours ago and hope this description can make it clear.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;




public class TestMouseEvent {
    public void createUI(){
        JFrame frame = new JFrame("Test Mouse Event");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        MainPanel mainPanel = new MainPanel();

        mainPanel.setPreferredSize(new Dimension(800, 600));
        mainPanel.addMouseListener(new ImageMouseListener());
        mainPanel.addMouseMotionListener(new ImageMouseAdapter());

        frame.add(mainPanel,BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        TestMouseEvent testMouseEvent = new TestMouseEvent();
        testMouseEvent.createUI();
    }

    @SuppressWarnings("serial")
    class MainPanel extends JPanel{
        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setFont(new Font("Arial", Font.PLAIN, 20));
            g.drawString("I'm a panel and I'm being listened now", 200, 300);
        }
    }

    class ImageMouseListener implements MouseListener{

        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub
            System.out.println("clicked");
        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub
            System.out.println("pressed");
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub
            System.out.println("released");     
            JOptionPane.showMessageDialog(null, "I only want to be showed when \"drag\" event over but not for click event!");
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }


    }

    class ImageMouseAdapter extends MouseAdapter{       
        public void mouseDragged(MouseEvent e){
            if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
                System.out.println("dragged");              
            }
        }
    }
}

I only want to show the JOptionPane right after the drag operation but not the click operation.So how can I know the difference?

Eugene
  • 10,627
  • 5
  • 49
  • 67

2 Answers2

3

Inside your ImageMouseAdapter set a flag (let's name if dragInProgressFlag) to true. On release check the flag. If it's true show the JOptionPane and reset it back to false.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
1

First, unify your MouseListener and MouseMotionListener into a single class, you can do this simply by using MouseAdapter...

Second, add a instance variable to act as a flag to indicate if a drag operation is taking place...

class ImageMouseListener extends MouseAdapter {

    private boolean isDragging = false;

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        System.out.println("clicked");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        if (isDragging) {
            System.out.println("released");
            JOptionPane.showMessageDialog(null, "I only want to be showed when \"drag\" event over but not for click event!");
        }
        isDragging = false
    }

    public void mouseDragged(MouseEvent e) {
        if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
            isDragging = true;
            System.out.println("dragged");
        }
    }
}

Create a single instance of the ImageMouseListener and add it as the mouseListener and mouseMotionListener for your panel...

ImageMouseListener handler = new ImageMouseListener();
mainPanel.addMouseListener(handler);
mainPanel.addMouseMotionListener(handler);

You could consider using the inbuilt Drag'n'Drop support within in Java, depending on what you want to achieve, for example, have a look at Java - How to drag and drop JPanel with its components

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366