-1

Using Java 1.8.

I use JInternalFrame. Here I set the size of the internal frame WelcomeInternalFrame.java .

public class WelcomeInternalFrame extends JInternalFrame implements ActionListener {
    private void addInternalFrame(JInternalFrame internalFrame, Boolean isMaximize, Dimension dimension) {
        desktop.add(internalFrame);
        try {
            internalFrame.setSelected(true);
            if (isMaximize != null && isMaximize) {
                internalFrame.setMaximum(true);
            } else {
                internalFrame.setSize(dimension);
            }
        } catch (java.beans.PropertyVetoException e) {
            logger.error(e.getMessage(), e);
        }
    }
}

In my internalFrame I use GridBagConstraints. The parent of my internalFrame is JFrame.

Here code of parent MainScreenView.java:

public class MainScreenView extends JFrame implements ActionListener {
    private JDesktopPane desktop;
    private void buildContent() {
            currentInternalFrame = new AccountsScroller(this);
            addInternalFrame(currentInternalFrame, true, null);
            setContentPane(desktop);

            desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            setLocationRelativeTo(null); // center
            setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
            setVisible(true);
        }
}

This code maximize internal frame, or set specific size. OK.

But I need also to center internal frame. How I can do this?

Alex
  • 705
  • 1
  • 9
  • 18
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Dec 23 '17 at 14:13
  • What about `setLocation`or `setBounds`? If you do not set the location of the internal frame, it will come up at 0,0 (the upper left of its container). You can use the setLocation or setBounds method to specify the upper left point of the internal frame, relative to its container. – Würgspaß Dec 23 '17 at 14:17

1 Answers1

1

This question is very much dependent on the LayoutManager(s) you use. If you do not use any LayoutManager, this works:

private void addInternalFrame(JInternalFrame internalFrame, Boolean isMaximize, Dimension dimension) {
   try {
       internalFrame.setSelected(true);
       if (isMaximize != null && isMaximize) {
           internalFrame.setMaximum(true);
       } else {
           internalFrame.setSize(dimension);
       }
   } catch (java.beans.PropertyVetoException e) {                      
         logger.error(e.getMessage(), e);
   }

    internalFrame.setLocation(desktop.getWidth()/2 - internalFrame.getWidth()/2,
        desktop.getHeight()/2 - internalFrame.getHeight()/2);
    desktop.add(internalFrame);
    internalFrame.setVisible(true);
 }

Telling from your other questions, I assume that desktopis of type JDesktopPane. But it would work with anything derived from java.awt.Container

Würgspaß
  • 4,660
  • 2
  • 25
  • 41
  • I use JDesktopPane desktop – Alex Dec 23 '17 at 15:38
  • Not help. My internal frame is on the left upper corner. – Alex Dec 23 '17 at 15:43
  • @Alex, This should work(1+). It is just basic math to calculate the location. If it doesn't work then that probably means you set a layout manager on the desktop pane which is wrong. You need to use a null layout. You can easily verify the calculation by doing basic debugging and displaying the value of `desktop.getLocation()`. – camickr Dec 23 '17 at 15:47
  • `desktop.getLocaton` return java.awt.Point[x=0,y=0] – Alex Dec 23 '17 at 17:42
  • @Alex, sorry, I made a typo. Please take the time to understand the "context" of the suggestion. Don't assume any posted code is always accurate. You are trying to change the location of the internal frame. So don't you think it would make sense to check the actual location of the internal frame so see if you calculation worked as expected? – camickr Dec 23 '17 at 19:41
  • I found solution: `Dimension dimensionScreen = Toolkit.getDefaultToolkit().getScreenSize(); Dimension weclomeSCreenDimension = new Dimension(500, 400); currentInternalFrame = new WelcomeInternalFrame(this); currentInternalFrame.setLocation( (int) ((dimensionScreen.getWidth() / 2) - (weclomeSCreenDimension.getWidth() / 2)), (int) ((dimensionScreen.getHeight() / 2) - (weclomeSCreenDimension.getHeight() / 2)));` – Alex Dec 24 '17 at 09:30