Skip to main content

JXLayer -- Issue with mouse event conversion

Please note these forums are being decommissioned and use the new and improved forums at
1 reply [Last post]
Joined: 2007-05-23

Hi, I have a JLabel that I am wrapping with a JXLayer. To get the label to scale, I alter the bounds of the layer and add the layer to a panel with a null layout manager. This works great. For example, if I add the layer to the panel and set the layer's bounds to be twice the width and twice the height of the label's preferred size, the label is scaled properly.
In addition to this, I want to allow the user to drag the label around the panel. I've added a mouse listener to the label and adjust the location of the layer as appropriate. Again, this works great. Except, if I scale the layer down to 50% of the width/height of the label's preferred size. When I do this, I start to get misconverted mouse events from PBTransform. That is, the location of the mouse event is incorrect.
Is there a solution for this problem?

Reply viewing options

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

Sorry, I didn't know I couldn't attach a java file to my message. Here's the source code:
import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;

import org.jdesktop.jxlayer.JXLayer;
import org.pbjar.jxlayer.demo.TransformUtils;

public class SwingTest {

* @param args
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setSize(800, 600);
JPanel panel = new JPanel(null);
JLabel label = new JLabel("<html>JXLayer<br>Drag<br>Test</html>");
final JXLayer<JComponent> layer = TransformUtils.createTransformJXLayer(label);
layer.setBounds(25, 25, (int) (label.getPreferredSize().width * 0.5),
(int) (label.getPreferredSize().height * 0.5));
new LabelMovementControl(layer, label);

private static class LabelMovementControl extends MouseInputAdapter {

private final JXLayer<JComponent> _layer;
private Point _mousePressed;

private LabelMovementControl(JXLayer<JComponent> layer, JLabel label) {
_layer = layer;

public void mousePressed(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)) {
_mousePressed = e.getPoint();

public void mouseDragged(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)) {
if(_mousePressed != null) {
Point oldLoc = _layer.getLocation();
int newX = oldLoc.x + e.getX() - _mousePressed.x;
int newY = oldLoc.y + e.getY() - _mousePressed.y;
Point newLoc = new Point(newX, newY);

public void mouseReleased(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)) {
_mousePressed = null;

public void mouseEntered(MouseEvent e) {

public void mouseExited(MouseEvent e) {