javacenterjinternalframejdesktoppane

Centering a JInternalFrame inside JDesktopPane not working correctly


I am attempting to create a JInternalPane inside of the JDesktopPane but it is not centering correctly.

Here is how the JDesktopPane is created (I am using Netbeans drag-and-drop):

JDesktopPane desktopPane;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0, 0, screenSize.width / 2, screenSize.height / 2);
desktopPane = new JDesktopPane();
setContentPane(desktopPane);

I then create the JInternalFrame:

LoginUI login = new LoginUI();
Dimension desktopSize = desktopPane.getSize();
Dimension loginSize = login.getSize();
int width = (desktopSize.width - loginSize.width) / 2;
int height = (desktopSize.height - loginSize.height) / 2;
login.setLocation(width, height);
login.setVisible(true);
desktopPane.add(login);
try {
    login.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}

I also set the preferred size of the JInternalFrame.

The login frame, however, appears in the top-left of the desktopPane, with most of it not visible (i.e. "outside" of the desktopPane).

I mostly followed this Java documentation. I also got the setLocation() information from this post as well as this post.

What am I doing wrong here that is causing the JInternalFrame to not be cenetered? Any help is appreciated.


Solution

  • Based on the available information, I'd say nothing, which leads me to believe it has something to do with what you're not showing us.

    For example, if I take the basic information you've posted and paste into a runnable example, it works fine

    Internal Frame

    import java.awt.Dimension;
    import java.awt.EventQueue;
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JDesktopPane dp = new JDesktopPane();
                    dp.setPreferredSize(new Dimension(200, 200));
    
                    JInternalFrame iFrame = new JInternalFrame("Test", true, true, true, true);
                    iFrame.getContentPane().setPreferredSize(new Dimension(100, 100));
                    iFrame.pack();
                    iFrame.setVisible(true);
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(dp);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
    
                    dp.add(iFrame);
    
                    Dimension desktopSize = dp.getSize();
                    Dimension loginSize = iFrame.getSize();
    
                    int x = (desktopSize.width - loginSize.width) / 2;
                    int y = (desktopSize.height - loginSize.height) / 2;
                    iFrame.setLocation(x, y);
    
                    frame.setVisible(true);
                }
            });
        }
    
    }
    

    This suggests that there is something in your code which you're not sharing which is causing your issues

    Consider providing a runnable example which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses