Note that there are some explanatory texts on larger screens.

plurals
  1. POFire mouse event on underlying components
    primarykey
    data
    text
    <p>I'm looking for a way to pass mouse events to components covered by other components. To illustrate what I mean, here's a sample code. It contains two <em>JLabel</em>s, one is twice smaller and entirely covered with a bigger label. If you mouse over the labels, only the bigger one fires <em>mouseEntered</em> event however.</p> <pre><code>import java.awt.Color; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.WindowConstants; import javax.swing.border.LineBorder; public class MouseEvtTest extends JFrame { public MouseEvtTest() { setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLayout(null); setSize(250, 250); MouseAdapter listener = new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { System.out.printf("Mouse entered %s label%n", e.getComponent().getName()); } }; LineBorder border = new LineBorder(Color.BLACK); JLabel smallLabel = new JLabel(); smallLabel.setName("small"); smallLabel.setSize(100, 100); smallLabel.setBorder(border); smallLabel.addMouseListener(listener); add(smallLabel); JLabel bigLabel = new JLabel(); bigLabel.setName("big"); bigLabel.setBorder(border); bigLabel.setSize(200, 200); bigLabel.addMouseListener(listener); add(bigLabel, 0); //Add to the front } public static void main(String[] args) { new MouseEvtTest().setVisible(true); } } </code></pre> <p>What would be the best way to fire mouse entered event on the smaller label when cursor is moved to the coordinates above it? How would it work in case where there would be multiple components stacked on top of each other? What about the remaining mouse events, like <em>mouseClicked</em>, <em>mousePressed</em>, <em>mouseReleased</em>, etc.?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload