The problem is when I change tabs, the objects reposition themselves to the coordinates I had initially set them to.
You need to use a null layout on the panel that contains the components you are dragging:
panel.setLayout( null );
题
I have some movable objects (JLabel
s) and the user will reposition them within the parent ChartPanel
during runtime. To move the objects I've implemented the (really helpful) ComponentMover
class, found here: http://tips4java.wordpress.com/2009/06/14/moving-windows/).
As they are moved, their (x,y) coordinates are displayed back to the user. I'm using a JTabbedPane
and the coordinates of two objects are displayed per tab (I tried to post a screenshot but my lack of reputation won't allow it).
The problem is when I change tabs, the objects reposition themselves to the coordinates I had initially set them to. I think this is something to do with revalidate()
or repaint()
?
I tried to implement a ChangeListener
public static final ChangeListener
relocateObjects = new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
labelPole.setAlignmentX(polePositionX);
labelPole.setAlignmentY(polePositionY);
labelZero.setAlignmentX(zeroPositionX);
labelZero.setAlignmentY(zeroPositionY);
}
};
on the JTabbedPane
tabbedPane.addChangeListener(ComponentMover.relocateObjects);
to relocate the object to coordinates stored just before the event, but to no avail.
I tried to find a way to prevent the objects from listening (and thus responding) to a state change or simply update their default positions to override their initial coordinates, which you'd think would be an option, but I have failed.
I've literally spent all day on this, so I would love a solution or a push in the right direction. Thank you.
解决方案
The problem is when I change tabs, the objects reposition themselves to the coordinates I had initially set them to.
You need to use a null layout on the panel that contains the components you are dragging:
panel.setLayout( null );