javaswingjpaneljdesktoppane

Automatically resize JPanel without use BorderLayout.CENTER


I have a JPanel inside a JDesktopPane and I need to resize the panel automatically when the size of desktopPane changes.

The size of panel always needs to be the same of desktopPane. I can't use BorderLayout.CENTER because if I use it, I cant resize others frames inside of desktopPane.

Thanks


Solution

  • Use a ComponentListener/Adapter

    JDesktopPane desktop = ...;
    JPanel p = ...;
    
    ComponentListener cl = new ComponentAdapter() {
      public void componentResized(ComponentEvent ce) {
        //reset your panel size here
      }
    }
    
    desktop.addComponentListener(cl);