Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want the <code>mouseEntered</code> and <code>mouseExited</code> to be called on full boundaries. This is, as you have noticed, not directly possible with the "normal" <code>MouseListener</code>. </p> <p>The simplest way is to add the listener to all child-components of the panel:</p> <pre><code>private static void addListenerToAllComponents(JComponent c, MouseListener l) { c.addMouseListener(l); for (Component cc : c.getComponents()) if (cc instanceof JComponent) addListenerToAllComponents((JComponent) cc, l); } </code></pre> <p><strong>Full example:</strong></p> <pre><code>public static void main(String[] args) { final JFrame frame = new JFrame("Test"); frame.add(new JLabel("Testing"), BorderLayout.NORTH); final JPanel panel = new JPanel(new GridLayout(2, 1)); panel.setBackground(Color.RED); MouseListener l = new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { panel.setBackground(Color.BLUE); } @Override public void mouseExited(MouseEvent e) { panel.setBackground(Color.RED); } }; panel.add(new JLabel("Hello")); panel.add(new JTextField("World!")); addListenerToAllComponents(panel, l); frame.add(panel, BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); frame.setVisible(true); } </code></pre> <hr> <h2>Another workaround (previous answer)...</h2> <p>...is to set a <a href="http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html#glasspane" rel="nofollow">GlassPane</a> and check bounds yourself:</p> <pre><code>public static void main(String[] args) { JFrame frame = new JFrame("Test"); frame.add(new JLabel("Testing"), BorderLayout.NORTH); final JPanel panel = new JPanel(new GridLayout(2, 1)); frame.add(panel, BorderLayout.CENTER); panel.add(new JLabel("Hello")); panel.add(new JTextField("World!")); class GlassPane extends JComponent { GlassPane(final JComponent c) { addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseMoved(MouseEvent e) { Point p = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), c); if (c.contains(p)) c.setBackground(Color.BLUE); else c.setBackground(Color.RED); } }); addMouseListener(new MouseAdapter() { public void mouseExited(MouseEvent e) { c.setBackground(Color.MAGENTA); } }); } } GlassPane glass = new GlassPane(panel); frame.setGlassPane(glass); glass.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); frame.setVisible(true); } </code></pre> <hr> <p>There are some stuff you need to look in to... Event redistribution is one and might be a problem in your case. Follow <a href="http://download.oracle.com/javase/tutorial/uiswing/examples/components/GlassPaneDemoProject/src/components/GlassPaneDemo.java" rel="nofollow">this example</a> and implement an event distribution listener on the glass pane:</p> <pre><code>MouseInputListener i = new MouseInputListener() { private void redispatchMouseEvent(MouseEvent e) { Point glassPanePoint = e.getPoint(); Container container = frame.getContentPane(); Point containerPoint = SwingUtilities.convertPoint( GlassPane.this, glassPanePoint, container); Component component = SwingUtilities.getDeepestComponentAt( container, containerPoint.x, containerPoint.y); if (component != null) { Point componentPoint = SwingUtilities.convertPoint( GlassPane.this, glassPanePoint, component); component.dispatchEvent(new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(), componentPoint.x, componentPoint.y, e.getClickCount(), e.isPopupTrigger())); } } public void mouseMoved(MouseEvent e) { redispatchMouseEvent(e); } public void mouseDragged(MouseEvent e) { redispatchMouseEvent(e); } public void mouseClicked(MouseEvent e) { redispatchMouseEvent(e); } public void mouseEntered(MouseEvent e) { redispatchMouseEvent(e); } public void mouseExited(MouseEvent e) { redispatchMouseEvent(e); } public void mousePressed(MouseEvent e) { redispatchMouseEvent(e); } public void mouseReleased(MouseEvent e) { redispatchMouseEvent(e); } }; addMouseListener(i); addMouseMotionListener(i); </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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