Skip to main content

JXLayer -- Issue with mouse event conversion

1 reply [Last post]
sirstruck
Offline
Joined: 2007-05-23
Points: 0

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?
Thanks,
David

Reply viewing options

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

Sorry, I didn't know I couldn't attach a java file to my message. Here's the source code:
<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);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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);
panel.add(layer);
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private static class LabelMovementControl extends MouseInputAdapter {

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

private LabelMovementControl(JXLayer<JComponent> layer, JLabel label) {
_layer = layer;
label.addMouseListener(this);
label.addMouseMotionListener(this);
}

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

@Override
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);
_layer.setLocation(newLoc);
}
}
}

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

@Override
public void mouseEntered(MouseEvent e) {
_layer.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
}

@Override
public void mouseExited(MouseEvent e) {
_layer.setCursor(null);
}

}

}

</code>