Skip to main content

Mouse events & blocking

3 replies [Last post]
Joined: 2006-12-28

Hi Alex

Two things this time around.

1. It seems mouse events are forwarded to LayerUI twice.
See attached code.

2. I would like to have a method that defines a dynamic area
where where mouse events are passed to the LayerUI, but not to
underlying components. I did not find a way to achieve this. See code.

Best reagards


<p>import org.jdesktop.jxlayer.JXLayer;<br />
import org.jdesktop.jxlayer.plaf.AbstractLayerUI;</p>
<p>import javax.swing.*;<br />
import java.awt.*;<br />
import java.awt.event.MouseEvent;<br />
import java.awt.event.MouseWheelEvent;<br />
import java.awt.geom.Rectangle2D;</p>
<p>/**<br />
 * This program demonstrates two problems with JXLayer - I think...<br />
 * 1. Mouse events are duplicated. Easiest way to see it is if you have a mouse wheel, move the mouse wheel one position.<br />
 *    and you will see the counter update twice. I believe this has to do with the implementation of JXLayer, where you have<br />
 *    both the processEvent method and LayerInputContext class.<br />
 *<br />
 * 2. Drag the shaded rectangle over one of the text fields, and release. The cursor changes to a text cursor.<br />
 *    Drag the rectangle out, the cursor remains a text cursor until you release the mouse. What I want to be able to<br />
 *    achieve, is for the cursor not change when over random text fields. Is there a smart way to do this?<br />
 *<br />
 * @author skjalg.bjorndal<br />
 * @since 21.jul.2008<br />
 */<br />
public class MouseTrap extends AbstractLayerUI {</p>
<p>    Rectangle2D.Double mouseCover = new Rectangle2D.Double(100, 100, 200, 50);<br />
    Color coverColor = new Color(128, 128, 128, 128);<br />
    int wheelCount;</p>
<p>    protected void paintLayer(Graphics2D g2, JXLayer l) {<br />
        super.paintLayer(g2, l);</p>
<p>        g2.setColor(coverColor);<br />
        g2.fill(mouseCover);<br />
        g2.setColor(;<br />
        g2.drawString("Wheel count " + wheelCount, 20, 20);<br />
<p>    protected void processMouseMotionEvent(MouseEvent event, JXLayer jxLayer) {<br />
        Point point = SwingUtilities.convertPoint(event.getComponent(), event.getPoint(), jxLayer);</p>
<p>        switch (event.getID()) {<br />
            case MouseEvent.MOUSE_DRAGGED:<br />
                mouseCover.setRect(point.getX() - 100, point.getY() - 25, 200, 50);<br />
                setDirty(true);<br />
        }<br />
<p>    protected void processMouseWheelEvent(MouseWheelEvent event, JXLayer jxLayer) {<br />
        super.processMouseWheelEvent(event, jxLayer);<br />
        wheelCount++;<br />
        setDirty(true);<br />
<p>    public static void main(String[] args) {</p>
<p>        SwingUtilities.invokeLater(new Runnable() {<br />
            public void run() {<br />
                JPanel view = new JPanel(new GridLayout(3, 2));<br />
                view.add(new JLabel("Label 1"));<br />
                view.add(new JTextField("Text 1"));<br />
                view.add(new JLabel("Label 2"));<br />
                view.add(new JTextField("Text 2"));<br />
                view.add(new JLabel("Label 3"));<br />
                view.add(new JTextField("Text 3"));</p>
<p>                JFrame frame = new JFrame();<br />
                JXLayer layer = new JXLayer(view);<br />
                layer.setUI(new MouseTrap());<br />
                frame.getContentPane().add(layer);<br />
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />
                frame.setSize(640, 480);<br />
<p>                frame.setVisible(true);<br />
            }<br />
        });<br />

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2005-09-05

Hello Skjalg

The duplicated events is a good catch!
fixed now, try the latest jar

The smartest way to do what you need is to implement your translucent rectangular
as a non-opaque component and place it to the layer's glassPane
(inside your custom LayerUI, certainly)

This way you'll be able to set any mouse cursor to this component
and it won't interfere with underlying components.

If you really need to block the mouseEvents for the underlying components
(which is not the best solution for your case), call mouseEvent.consume()


Joined: 2006-12-28

Rewrting it as a component works.



Joined: 2005-09-05

Congratulations, you became one of the most advanced JXLayer users!