Swing MouseDragged auf Containerebene?

Landei

Top Contributor
Folgendes Problem: Ich habe einen Container mit Null-Layout und darin Unterkomponenten. Nun würde ich gerne die MouseDrag-Ereignisse im Container behandeln, auch wenn das Draggen auf der Unterkomponente gestartet wird, weil ich nur da alle nötigen Informationen habe. Gibt es da einen Trick?
 
container.dispatchEvent(evt);
Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DispatchEventDemo extends JFrame {
    private JPanel container;
    private final JLabel jLabel;
    private final JLabel lbLocation;
    private final JLabel lbSource;
    public DispatchEventDemo() {
        super("DispatchEventDemo");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);
        container = new JPanel(null);
        container.setName("container");
        getContentPane().add(container, BorderLayout.CENTER);
        jLabel = new JLabel("Test");
        jLabel.setName("jLabel");
        jLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        jLabel.setBounds(10, 10, 100, 22);
        container.add(jLabel);
        MouseAdapter mouseHandlerLabel = new MouseAdapter() {
            @Override
            public void mouseDragged(final MouseEvent e) {
                Point point = e.getPoint();
                point.translate(jLabel.getX(), jLabel.getY());
                jLabel.setLocation(point);
                container.dispatchEvent(e);
            }
        };
        jLabel.addMouseMotionListener(mouseHandlerLabel);
        MouseAdapter mouseHandlerPanel = new MouseAdapter() {
            @Override
            public void mouseDragged(final MouseEvent e) {
                JComponent source = (JComponent) e.getSource();
                MouseEvent evt = SwingUtilities.convertMouseEvent(source, e, container);
                Point point = evt.getPoint();
                lbLocation.setText(point.x + ", " + point.y);
                lbSource.setText(source.getName());
            }
        };
        container.addMouseMotionListener(mouseHandlerPanel);
        //
        JLabel lbLoc = new JLabel("Current drag position: ");
        lbLoc.setBounds(150, 10, 140, 22);
        container.add(lbLoc);
        lbLocation = new JLabel();
        lbLocation.setBounds(290, 10, 100, 22);
        container.add(lbLocation);
        //
        JLabel lbSrc = new JLabel("Source: ");
        lbSrc.setBounds(150, 30, 140, 22);
        container.add(lbSrc);
        lbSource = new JLabel();
        lbSource.setBounds(290, 30, 100, 22);
        container.add(lbSource);
    }
    public static void main(final String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DispatchEventDemo().setVisible(true);
            }
        });
    }
}
 
Zuletzt bearbeitet:
Die dispatch-Variante funktioniert einwandfrei, wobei man mit den Koordinaten aufpassen muss (also wenn das Event von einem Kind kommt, noch dessen Location dazurechnen muss). Wieder was gelernt, danke!
 

Zurück
Oben