I am making an apllication in java swing for practice by using drag and drop components , i am facing a problem, i put inside the JPanel, an JInternal Frame
, it is moveable,
i want to make it un-moveable
,which event is used for it? How can i do it ? i search on the internet but unfortunately unable to find the solution for this.
I have found this on internet but i want to do it with the help of event option in swing.
// Make first internal frame unmovable
JInternalFrame[] frames = frame.desktop.getAllFrames();
JInternalFrame f = frames[0];
BasicInternalFrameUI ui = (BasicInternalFrameUI)f.getUI();
Component north = ui.getNorthPane();
MouseMotionListener[] actions = // there is no option for MouseMotionListener in the event option
(MouseMotionListener[])north.getListeners(MouseMotionListener.class);
for (int i = 0; i < actions.length; i++)
north.removeMouseMotionListener( actions[i] );
DesktopManager
is resposible for all operation with internal frames. So you can redefine it using the proxy pattern to make some of your frames non-movable. Here is non-complete example for you (sorry I havn't tested this code, so I'm not sure whether it works).
private static class ProxyDesktopManager implements DesktopManager {
private final DesktopManager delegate;
public ProxyDesktopManager(DesktopManager delegate) {
this.delegate = Objects.requireNonNull(delegate);
}
// Check whether frame is moveable
private boolean checkFrameMovable(JComponent frame) {
if (frame instanceof JInternalFrame) {
// TODO check whether the frame if movable
}
return false;
}
@Override
public void beginDraggingFrame(JComponent f) {
if (checkFrameMovable(f)) {
delegate.beginDraggingFrame(f);
}
}
@Override
public void dragFrame(JComponent f, int newX, int newY) {
if (checkFrameMovable(f)) {
delegate.dragFrame(f, newX, newY);
}
}
@Override
public void endDraggingFrame(JComponent f) {
if (checkFrameMovable(f)) {
delegate.endDraggingFrame(f);
}
}
@Override
public void openFrame(JInternalFrame f) {
delegate.openFrame(f);
}
@Override
public void closeFrame(JInternalFrame f) {
delegate.closeFrame(f);
}
@Override
public void maximizeFrame(JInternalFrame f) {
delegate.maximizeFrame(f);
}
@Override
public void resizeFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
delegate.resizeFrame(f, newX, newY, newWidth, newHeight);
}
// IMPORTANT: simply delegate all another methods like openFrame or
// resizeFrame
}
Now you can use this proxy class for your JDesktopPane
JDesktopPane desktop = new JDesktopPane();
desktop.setDesktopManager(new ProxyDesktopManager(desktop.getDesktopManager()));