Search code examples
javaswingjinternalframejdesktoppane

why does JInternalFrame's setSize not work


I make a JFrame, and add a JDesktopPane into the JFrame, and then add a JInternalFrame into the JDesktopPane, the JInternalFrame' initial size is the same as JDesktopPane. When the JFrame is maxmized, the JDesktopPane will also be maximized, but the JInternalFrame is not maximized. And I want that the JInternalFrame is maximized when the JFrame is maximized, so I add a WindowStateListener for the JFrame, when the JFrame is maximized,I will call JInternalFrame' setSize method to make the JInternalFrame' size is the same as the JDesktopPane, the code is as follows:

addWindowStateListener(new java.awt.event.WindowStateListener() {

   public void windowStateChanged(java.awt.event.WindowEvent evt) {
       jInternalFrame.setSize(jDesktopPane1.getWidth(), 
           jDesktopPane1.getHeight());
   }
});

but it does not work. but when I click to maximize the JFrame, and then I click to minimize the JFrame, and then click taskbar to let the JFrame visible, and it does work.

Why it happen like this?


Solution

  • I would use a ComponentListener on the JDesktopPane and listen for the componentResized event. Then whenever the desktop panes size changes, either by maximize/restore or by resizing the frame you will be notified.