2

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Andy Jiang
  • 23
  • 4

3 Answers3

2

Do you call repaint on the container that holds the JInternalFrame, the JDesktopPane? If not, you should.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I called jDesktopPane1.repaint() after setSize, but it still like I said before. – Andy Jiang Nov 15 '11 at 12:00
  • I got it. I override the JFrame's paint method, and call setSize method in the paint method, it does work. Thanks! – Andy Jiang Nov 15 '11 at 12:31
  • 3
    No, no, no, you should not be overriding the frames paint() method for this. The paint method is for painting. It is not for changing properties of the components added to the frame. – camickr Nov 15 '11 at 16:09
1

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

It sounds like you just want the JInternalFrame to follow the state of the JFrame. Why don't you just set the JInternalFrame to be maximized as well?

jInternalFrame.setMaximum( true );
wolfcastle
  • 5,850
  • 3
  • 33
  • 46