0

So I have an desktopPane on a JFrame, on the desktopPane is a JInternalFrame. Now I set the location of the internalFrame to the center of the desktopPane, but when I resize the whole Frame the InternalFrame stays where it was and does not change the position to the new center.

public void openLogin(){
    JInternalFrame iFrame = new LoginScreen(appMain);
    desktopPane =new JDesktopPane();
    add(desktopPane);
    frameSize = getSize();
    desktopPane.setSize(frameSize);
    Dimension desktopSize = desktopPane.getSize();
    Dimension jInternalFrameSize = iFrame.getSize();
    iFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);
    desktopPane.add(iFrame);
}

This is the method where I set the location, how to change it so it matches the new center? I tried it with some listeners but nothing works right.


Ok, so because I have no time to use the answer from @Hovercraft full of Eels, I tried again a componentlistener:

public void openLogin(){
    JInternalFrame iFrame = new LoginScreen(appMain);
    desktopPane =new JDesktopPane();
    add(desktopPane);
    frameSize = getSize();
    newSize=frameSize;
    desktopPane.addComponentListener(new ComponentListener() {

        @Override
        public void componentShown(ComponentEvent e) {

        }

        @Override
        public void componentResized(ComponentEvent e) {
            newSize = getSize();
        }

        @Override
        public void componentMoved(ComponentEvent e) {

        }

        @Override
        public void componentHidden(ComponentEvent e) {

        }
    });
    if(newSize != frameSize){
        desktopPane.setSize(newSize);
        Dimension desktopSize = desktopPane.getSize();
        Dimension jInternalFrameSize = iFrame.getSize();
        iFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);
        desktopPane.add(iFrame);

    }
    else{
        desktopPane.setSize(frameSize);
        Dimension desktopSize = desktopPane.getSize();
        Dimension jInternalFrameSize = iFrame.getSize();
        iFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);
        desktopPane.add(iFrame);

    }

}

It does not even trigger the Listener. @Hovercraft, I will try your thing but not today, nonthereless thank you very much!

Ok I found my fail, it works now:

public void openLogin(){
    iFrame = new LoginScreen(appMain);
    desktopPane =new JDesktopPane();
    add(desktopPane);
    frameSize = getSize();
    newSize=frameSize;
    addComponentListener(new ComponentListener() {

        @Override
        public void componentShown(ComponentEvent e) {

        }

        @Override
        public void componentResized(ComponentEvent e) {
            newSize = getSize();
            System.out.println("blaaaa");
            desktopPane.setSize(newSize);
            Dimension desktopSize = getSize();
            Dimension jInternalFrameSize = iFrame.getSize();
            iFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);

        }

        @Override
        public void componentMoved(ComponentEvent e) {

        }

        @Override
        public void componentHidden(ComponentEvent e) {

        }
    });

    desktopPane.setSize(frameSize);
    Dimension desktopSize = desktopPane.getSize();
    Dimension jInternalFrameSize = iFrame.getSize();
    iFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);
    desktopPane.add(iFrame);



}
  • 1) Are you sure that you want to be using JDesktopPane and JInternalFrame for this? I ask as this behavior suggests that you simply want a JPanel or something similar centered in your top-level window, the JFrame. 2) If you tried using some listeners, consider posting your best attempt here and describing for us just what it may be doing wrong or right. 3) Options include using a ComponentListener and responding to resize events, or 4) giving the JDesktopPane some layout to center the component such as a GridBagLayout, but then again this suggests not using a JDesktopPane/JInternalFrame. – Hovercraft Full Of Eels Aug 15 '14 at 12:27
  • I have to use it like this, because after the Login(InternalFrame) the main Frame disposes the desktopPane and loads other elements like Jlists etc. So if I use the component listener where do I place it? Inside this method or somewhere else? – Maciej Laton Aug 15 '14 at 12:32

2 Answers2

1

You state in comment:

I have to use it like this, because after the Login(InternalFrame) the main Frame disposes the desktopPane and loads other elements like Jlists etc. So if I use the component listener where do I place it? Inside this method or somewhere else?

No you don't.

  • Perhaps better would be to use a CardLayout to swap views.
  • This way you can display your login JPanel first,
  • You can center this JPanel in another JPanel by giving the wrapper JPanel a GridBagLayout, and then adding your login JPanel to it in a default way (without GridBagConstraints)
  • And then add this wrapper JPanel to your CardLayout using JPanel.
  • and then on successful login, swap to the work JPanel.

Another option:

  • Use a modal dialog such as a modal JDialog or a JOptionPane to do the login.
  • Display an introduction JPanel in the main GUI
  • Display the dialog above this, again making it modal, so that the user cannot interact with the main GUI until the dialog has been dealt with.
  • When the dialog is no longer visible, program flow returns to the calling code.
  • Here is where you check the results entered into the dialog which will tell you whether to swap from the intro JPanel to your work JPanel.
  • Again, use CardLayout to help you swap.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • When I start the application I load a JFrame, than this method above adds the desktopPane with the InternalFrame. When the login is succesfull, I remove the desktopPane and add an JPanel with a BorderLayout, to this I add a JPanel with a FlowLayout and to both Panels some components. Sorry I lack the ability to express myself right now, somehow -.- – Maciej Laton Aug 15 '14 at 12:46
  • 1
    @MaciejLaton: you express yourself fine. I still stand by my recommendation, as I see absolutely no reason to use a JDesktopPane/JInternalFrame here. I would either use a CardLayout, or some modal JDialog, and in fact would probably favor the latter. – Hovercraft Full Of Eels Aug 15 '14 at 12:48
0

You were part-way there with the ComponentListener. In the componentResized(...) method you need to adjust the location of the JInternalFrame.

Keep in mind, if the user moves the JInternalFrame on the desktop, it will get repositioned automatically if the JFrame is resized. This may not be desirable.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JInternalFrameDemo implements Runnable
{
  private JInternalFrame iFrame;
  private JDesktopPane desktop;

  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new JInternalFrameDemo());
  }

  public void run()
  {
    desktop = new JDesktopPane();
    desktop.setOpaque(true);
    desktop.addComponentListener(new ComponentListener()
    {
      public void componentResized(ComponentEvent e)
      {
        adjustInternalFrameLocation();
      }

      public void componentMoved(ComponentEvent e) {}
      public void componentShown(ComponentEvent e) {}
      public void componentHidden(ComponentEvent e) {}
    });

    JFrame frame = new JFrame("Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setContentPane(desktop);
    frame.setVisible(true);

    iFrame = new JInternalFrame("Centered InternalFrame");
    iFrame.setSize(200, 150);
    iFrame.setVisible(true);
    desktop.add(iFrame);

    adjustInternalFrameLocation();
  }

  private void adjustInternalFrameLocation()
  {
    Dimension desktopDim = desktop.getSize();
    Dimension iFrameDim = iFrame.getSize();

    int x = (desktopDim.width - iFrameDim.width) / 2;
    int y = (desktopDim.height - iFrameDim.height) / 2;

    iFrame.setLocation(x, y);
  }
}
splungebob
  • 5,357
  • 2
  • 22
  • 45