So I have an desktopPane on a JFrame, on the desktopPane is a JInternalFrame. Now I set the location of the internalFrame to the center of the desktopPane, but when I resize the whole Frame the InternalFrame stays where it was and does not change the position to the new center.
public void openLogin(){
JInternalFrame iFrame = new LoginScreen(appMain);
desktopPane =new JDesktopPane();
add(desktopPane);
frameSize = getSize();
desktopPane.setSize(frameSize);
Dimension desktopSize = desktopPane.getSize();
Dimension jInternalFrameSize = iFrame.getSize();
iFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);
desktopPane.add(iFrame);
}
This is the method where I set the location, how to change it so it matches the new center? I tried it with some listeners but nothing works right.
Ok, so because I have no time to use the answer from @Hovercraft full of Eels, I tried again a componentlistener:
public void openLogin(){
JInternalFrame iFrame = new LoginScreen(appMain);
desktopPane =new JDesktopPane();
add(desktopPane);
frameSize = getSize();
newSize=frameSize;
desktopPane.addComponentListener(new ComponentListener() {
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentResized(ComponentEvent e) {
newSize = getSize();
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
});
if(newSize != frameSize){
desktopPane.setSize(newSize);
Dimension desktopSize = desktopPane.getSize();
Dimension jInternalFrameSize = iFrame.getSize();
iFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);
desktopPane.add(iFrame);
}
else{
desktopPane.setSize(frameSize);
Dimension desktopSize = desktopPane.getSize();
Dimension jInternalFrameSize = iFrame.getSize();
iFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);
desktopPane.add(iFrame);
}
}
It does not even trigger the Listener. @Hovercraft, I will try your thing but not today, nonthereless thank you very much!
Ok I found my fail, it works now:
public void openLogin(){
iFrame = new LoginScreen(appMain);
desktopPane =new JDesktopPane();
add(desktopPane);
frameSize = getSize();
newSize=frameSize;
addComponentListener(new ComponentListener() {
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentResized(ComponentEvent e) {
newSize = getSize();
System.out.println("blaaaa");
desktopPane.setSize(newSize);
Dimension desktopSize = getSize();
Dimension jInternalFrameSize = iFrame.getSize();
iFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
});
desktopPane.setSize(frameSize);
Dimension desktopSize = desktopPane.getSize();
Dimension jInternalFrameSize = iFrame.getSize();
iFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);
desktopPane.add(iFrame);
}