I want to immobilize a JInternalFrame (who'd tought it? ;-) At the moment
I do it like this:
// in a class extending JInternalFrame itself:
this.addComponentListener(new ComponentAdapter() {
publicvoid componentMoved(ComponentEvent ce) {
if (locked) {
if (point == null) point= getLocation();
setLocation(point);
}
}
});
'point' and 'locked' are just private member variables. This works but it
looks funny, i.e. it's as if the JInternalFrame is fighting back: it moves a
bit but jumps back to its original location when you don't pay attention for
a split second.
It makes me giggle but my customers find it sloppy. They are used to
these neat little pins you can use to keep a frame pinned to where it is
at that moment. Is there anything like that possible using Swing? Of
course I scrutinized the API docs but I could't find anything I could use.
I don't care about a visible pin, if only that JInternalFrame would stand
still completely.
Thank you very much for that. That's what I call 'component surgery' ;-)
I've implemented a little 'toggle' method that removes or reinstalls those
listeners according to a simple boolean flag. As far as I can see now
everything works fine. Thank you very much again and
The latest 3.1dev drop of Substance look-and-feel allows you to set the SubstanceLookAndFeel.PERMANENTLY_PINNED client property on the JInternalFrame. If you set it to Boolean.TRUE, the relevant frame would be "pinned" - you can't move it or resize it.
In general, it's not a good idea to remove the listeners not installed by you, since you don't know what functionality you're removing - this can cause unexpected behaviour at least or exceptions / frame freezing in worse cases.
While looking at the source code of the installed
MouseMotionListener to see exactly what it did, I came up with a "better" (?) approach:
Very nice solution.
However Jos, keep in mind that whatever solution is used, the frame would also have to be resizable = false, or the user could work around it by just dragging the edges until the frame was back to it's original shape but in a different location.
As opposed to forcing everyone to use the Substance
LAF?
"Forcing" is a strong word here. I'm just following the often-voiced requests and adding them to Substance. Just pointing out that the solution with installing a custom desktop manager is not cross-LAF and may interfere with native fidelity, as well as the solution with removing mouse listeners that may result in unpredictable behavior. Not to mention a possible ClassCastException if the specific InternalFrameUI implementation doesn't extend BasicInternalFrameUI (since it doesn't have to).
By the way - the solution provided by Substance takes care of both moving and resizing.
While looking at the source code of the installed
MouseMotionListener to see exactly what it did, I
came up with a "better" (?) approach:
Very nice solution.
I agree; I ended up with simple MouseMotionListeners that wrap the
original ones registered at the NORTH pane. Those wrappers can be
enabled and disabled at will. The downside is that I have to re-wrap
those listeners when the LAF changes ...
However Jos, keep in mind that whatever solution is used, the
frame would also have to be resizable = false, or the user could work
around it by just dragging the edges until the frame was back
to it's original shape but in a different location.
Yep, I completely 'freeze' the frame, i.e. disable the MouseMotionListeners
and make the JInternalFrame non-resizable, non-maximizable and non-
iconifiable. I do have to check out the 'Substance' UI (I never heard of it
before, but then again, I'm no GUI wizard ;-)
Nice informative thread, thanks to all repliers for that.