How to setVisible a JinternalFrame in the center of the JdesktopPane?

Zakaria Marrah picture Zakaria Marrah · Apr 6, 2012 · Viewed 10k times · Source

Actually i wanna show the JinternalFrame in the center of the JDesktopPane and i used this methode as i use it in Jframes but it didn't work :

Extraction ex=new Extraction();//Extraction is the name of the JintenalFrame jDesktopPane1.add(ex); ex.setLocationRelativeTo(this); ex.setVisible(true);

So i am asking if there is another methode so i can display the JinternalFrame in the center of the JdesktoPane. Thank you

Answer

Akna picture Akna · Oct 31, 2012

Try something like :

JDesktopPane mainPanel;
JInternalFrame jif_test = new JInternalFrame();

public void centerJIF(JInternalFrame jif) {
    Dimension desktopSize = mainPanel.getSize();
    Dimension jInternalFrameSize = jif.getSize();
    int width = (desktopSize.width - jInternalFrameSize.width) / 2;
    int height = (desktopSize.height - jInternalFrameSize.height) / 2;
    jif.setLocation(width, height);
    jif.setVisible(true);
}

And

centerJIF(jif_test);