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.
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
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