Re: Passing a mouse event message to the parent window?
Sample compilable code:
I can drag the JLabel, but I can't drag the JButton, even when it is
disabled.
How do I get the mouse events to not be gobbled by the disabled
JButton?
OR How do I get the JButton to pass those mouse events to the parent
container?
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class DragSample extends JPanel
implements MouseListener, MouseMotionListener {
private JButton dragButton;
private JLabel label;
private JPanel parent;
private JComponent dragging = null;
private int mOffsetX, mOffsetY;
private boolean disabled = false;
public DragSample() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
JToolBar toolBar = buildToolbar();
add(toolBar);
parent = new JPanel();
add(parent);
parent.setBackground( Color.white);
parent.setPreferredSize(new Dimension(640, 480));
parent.addMouseMotionListener(this);
parent.addMouseListener(this);
// Create a button.
dragButton = new JButton("Drag Me");
parent.add(dragButton);
// Create JLabel
Border myBorder = BorderFactory.createLineBorder( Color.red );
label = new JLabel("Drag me");
label.setBounds(0, 100, 50, 50);
label.setBorder(myBorder);
parent.add(label);
}
private JToolBar buildToolbar() {
JToolBar toolBar = new JToolBar();
toolBar.setRollover( true );
toolBar.setFloatable( false );
JButton disableButton = new JButton("Disable");
disableButton.setToolTipText( "Disable 'Drag Me' Button" );
disableButton.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
disabled = true;
dragButton.setEnabled(false);
}
});
toolBar.add( disableButton );
return toolBar;
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("DragSample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new DragSample();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
//
============================================================================
// Mouse and Mouse motion event handlers
public void mouseDragged(MouseEvent e) {
if ( dragging!=null ) {
int newY = e.getY() - mOffsetY;
int newX = e.getX() - mOffsetX;
dragging.setLocation(newX, newY);
}
}
public void mouseMoved(MouseEvent e) {} //do nothing
public void mouseExited(MouseEvent e) {} // do nothing
public void mouseEntered(MouseEvent e) {} // do nothing
public void mouseClicked(MouseEvent e) {} // do nothing
public void mousePressed(MouseEvent e) {
int cx = e.getX();
int cy = e.getY();
System.out.println("MousePressed event");
// Find which component, if any, the mouse is over
int lx, ty, rx, by;
lx = label.getX();
rx = lx + label.getWidth();
if ((cx>=lx) && (cx<=rx)) {
ty = label.getY();
by = ty + label.getHeight();
if ((cy>=ty) && (cy<=by)) {
dragging = label;
}
}
lx = dragButton.getX();
rx = lx + dragButton.getWidth();
if ((cx>=lx) && (cx<=rx)) {
ty = dragButton.getY();
by = ty + dragButton.getHeight();
if ((cy>=ty) && (cy<=by)) {
dragging = dragButton;
}
}
if ( dragging != null ) {
lx = dragging.getX();
ty = dragging.getY();
rx = lx + dragging.getWidth();
by = ty + dragging.getHeight();
if ((cx>=lx) && (cx<=rx) && (cy>=ty) && (cy<=by)) {
mOffsetX = cx - lx; // offset from mouse pointer to loc
mOffsetY = cy - ty; // of region
}
}
}
public void mouseReleased(MouseEvent e) {
dragging = null;
}
}