0

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.

Ebad
  • 131
  • 11
  • `Toolkit.getDefaultToolkit().getScreenSize()` is a poor choice as it does not take into consideration system elements like the task bar or dock - you could use `setLocationRelativeTo` and pass it `null` – MadProgrammer Sep 08 '17 at 05:35
  • My guess is the `LoginUI` doesn't have a pre-defined size and is currently sized to `0x0` – MadProgrammer Sep 08 '17 at 05:52
  • @MadProgrammer - According to the first post that I linked the JInternalFrame does not have the setLocationRelativeTo(null) implementation. I tested it anyway and got an error. Also, the LoginUI (my JInternalFrame) does have a pre-defined size as I noted in my post. I can see part of the frame, it's just partially outside of the JDesktopPanel. If I set the location to, for example, setLocation(200,300), then the frame is completely visible within the JDesktopPane. – Ebad Sep 08 '17 at 16:53
  • *"According to the first post that I linked the JInternalFrame does not have the setLocationRelativeTo(null) implementation" - I wasn't taking about the `JInternalFrame`, I was talking about the `JFrame`. *"does have a pre-defined size as I noted in my post"* - I can see you calling `getSize`, but I don't see anywhere you've call `setSize` (or `pack`), because we I apply your code to an example it works – MadProgrammer Sep 08 '17 at 22:43

1 Answers1

1

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you. I had mine set up in a really weird way. Playing with your example showed me how to implement it better than what I had. Sometimes the drag-and-drop method can be more complicated than intended. – Ebad Sep 09 '17 at 15:32