I
Icarus
For a current project of mine I have to move some Labels around on the
GlassPane. For this, I am first putting the objects on the pane and
then move them to the right location using setLocation(). But the
setLocation() command is always ignored at first, until the next
MouseMotionEvent is intercepted.
Below is some sample code replicating the problem. Move the mouse in
the program window. Then use the mouse button, but don't move the
mouse. You will see the picture enter the window at the top, centered
vertically, even though the program sets the location to that of the
mouse cursor. If you now move the mouse, the program behaves correctly
again.
Do you have any idea how to correct this?
import java.awt.AWTEvent;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
@SuppressWarnings("serial")
public class PickUpAndDrop2 extends JFrame{
// Use Singleton design pattern to provide easy access of the
relevant object
// to the listeners
private static PickUpAndDrop2 instance = new PickUpAndDrop2();
// The label to be moved around
JLabel label;
private PickUpAndDrop2(){
this.setSize(200, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// Create a JLabel to be moved around on the GlassPane
java.net.URL url = this.getClass().getResource("image.png");
ImageIcon image = new ImageIcon(url);
this.label = new JLabel(image);
// Register a listener to the label to be picked up on click
this.label.addMouseListener(this.createListener());
// Get the GlassPane and prepare it for display
JPanel glasspane = (JPanel)this.getGlassPane();
glasspane.setOpaque(false);
// Put the label on the window
this.add(this.label);
}
// Puts the label on the GlassPane
protected void pickUp(){
// Create a new GlassPane
JPanel glasspane = new JPanel();
this.setGlassPane(glasspane);
glasspane.setVisible(true);
glasspane.setOpaque(false);
// Register GlassPane as an "indirect" listener to MouseMotionEvents
Toolkit.getDefaultToolkit().addAWTEventListener(this.createAWTEventListener(),
AWTEvent.MOUSE_MOTION_EVENT_MASK |
AWTEvent.MOUSE_EVENT_MASK);
// Put the label on the GlassPane
glasspane.add(this.label);
// Put the label on the current location of the mouse cursor
Point position = this.getMousePosition();
this.moveLabel(position);
}
// Move the label around
protected void moveLabel(Point position){
this.label.setLocation(position);
}
// Create a listener for putting the component the click occured on
on the GlassPane
private MouseAdapter createListener(){
MouseAdapter listener = new MouseAdapter(){
public void mouseClicked(MouseEvent event) {
PickUpAndDrop2.instance.pickUp();
}
};
return listener;
}
// Intercepts Events and checks whether or not there are events of
interest for the GlassPane
// without actually being a listener
private AWTEventListener createAWTEventListener(){
AWTEventListener listener = new AWTEventListener(){
@Override
public void eventDispatched(AWTEvent event) {
if (event instanceof MouseEvent) {
MouseEvent me = (MouseEvent) event;
// In case of a MouseEvent, move the object that's currently on
the GlassPane
// to the location of the event
Point point;
if (me.getID() == MouseEvent.MOUSE_EXITED &&
me.getComponent() == PickUpAndDrop2.instance) {
point = null;
} else {
MouseEvent converted =
SwingUtilities.convertMouseEvent(me.getComponent(), me,
PickUpAndDrop2.instance.getGlassPane());
point = converted.getPoint();
}
PickUpAndDrop2.instance.moveLabel(point);
}
}
};
return listener;
}
public static void main(String[] args){
PickUpAndDrop2 frame = PickUpAndDrop2.instance;
frame.setVisible(true);
}
}
P.s.: This is working on the results of an earlier message (see
http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/15f94143527b7745#),
but as it's a different problem, I am sending this as a separate
message. I hope this is okay.
GlassPane. For this, I am first putting the objects on the pane and
then move them to the right location using setLocation(). But the
setLocation() command is always ignored at first, until the next
MouseMotionEvent is intercepted.
Below is some sample code replicating the problem. Move the mouse in
the program window. Then use the mouse button, but don't move the
mouse. You will see the picture enter the window at the top, centered
vertically, even though the program sets the location to that of the
mouse cursor. If you now move the mouse, the program behaves correctly
again.
Do you have any idea how to correct this?
import java.awt.AWTEvent;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
@SuppressWarnings("serial")
public class PickUpAndDrop2 extends JFrame{
// Use Singleton design pattern to provide easy access of the
relevant object
// to the listeners
private static PickUpAndDrop2 instance = new PickUpAndDrop2();
// The label to be moved around
JLabel label;
private PickUpAndDrop2(){
this.setSize(200, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// Create a JLabel to be moved around on the GlassPane
java.net.URL url = this.getClass().getResource("image.png");
ImageIcon image = new ImageIcon(url);
this.label = new JLabel(image);
// Register a listener to the label to be picked up on click
this.label.addMouseListener(this.createListener());
// Get the GlassPane and prepare it for display
JPanel glasspane = (JPanel)this.getGlassPane();
glasspane.setOpaque(false);
// Put the label on the window
this.add(this.label);
}
// Puts the label on the GlassPane
protected void pickUp(){
// Create a new GlassPane
JPanel glasspane = new JPanel();
this.setGlassPane(glasspane);
glasspane.setVisible(true);
glasspane.setOpaque(false);
// Register GlassPane as an "indirect" listener to MouseMotionEvents
Toolkit.getDefaultToolkit().addAWTEventListener(this.createAWTEventListener(),
AWTEvent.MOUSE_MOTION_EVENT_MASK |
AWTEvent.MOUSE_EVENT_MASK);
// Put the label on the GlassPane
glasspane.add(this.label);
// Put the label on the current location of the mouse cursor
Point position = this.getMousePosition();
this.moveLabel(position);
}
// Move the label around
protected void moveLabel(Point position){
this.label.setLocation(position);
}
// Create a listener for putting the component the click occured on
on the GlassPane
private MouseAdapter createListener(){
MouseAdapter listener = new MouseAdapter(){
public void mouseClicked(MouseEvent event) {
PickUpAndDrop2.instance.pickUp();
}
};
return listener;
}
// Intercepts Events and checks whether or not there are events of
interest for the GlassPane
// without actually being a listener
private AWTEventListener createAWTEventListener(){
AWTEventListener listener = new AWTEventListener(){
@Override
public void eventDispatched(AWTEvent event) {
if (event instanceof MouseEvent) {
MouseEvent me = (MouseEvent) event;
// In case of a MouseEvent, move the object that's currently on
the GlassPane
// to the location of the event
Point point;
if (me.getID() == MouseEvent.MOUSE_EXITED &&
me.getComponent() == PickUpAndDrop2.instance) {
point = null;
} else {
MouseEvent converted =
SwingUtilities.convertMouseEvent(me.getComponent(), me,
PickUpAndDrop2.instance.getGlassPane());
point = converted.getPoint();
}
PickUpAndDrop2.instance.moveLabel(point);
}
}
};
return listener;
}
public static void main(String[] args){
PickUpAndDrop2 frame = PickUpAndDrop2.instance;
frame.setVisible(true);
}
}
P.s.: This is working on the results of an earlier message (see
http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/15f94143527b7745#),
but as it's a different problem, I am sending this as a separate
message. I hope this is okay.