8 Replies Latest reply: Sep 12, 2012 6:40 AM by 918178 RSS

    How to capture frame maximizing/minimizing event?

    918178
      I have a requirement.

      I have a JTable. The preferred size of the table should be set dynamically when the JFrame/window is maximized or minimized.

      If the window is maximized the preferred size should be

           table.setPreferredSize(new Dimension(1200, 370

      If the window is minimized then preferred size should be

           table.setPreferredSize(new Dimension(715, 370);

      I thought of a solution like if I capture maximize / minimizing event then I can set preferred size accordingly. I need some help
      to capture those events.

      Or if any other solution can be given for this requirement means also it is fine.

      Anybody guide me to capture frame maximizing / minimizing event
        • 1. Re: How to capture frame maximizing/minimizing event?
          jduprez
          You could add a ComponentListener to the Frame, leveraging componentResized(...) (http://docs.oracle.com/javase/6/docs/api/java/awt/event/ComponentListener.html#componentResized%28java.awt.event.ComponentEvent%29) to know about all resizing events, then use theFrame.getExtendedState() to detect whether the Frame is maximized or not.

          Note that I guess that by "minimized", you mean "not maximized but not iconified either" (on first read I mistook "minimized" for "iconified" and was a bit puzzled :))
          • 2. Re: How to capture frame maximizing/minimizing event?
            918178
            Thanks. I will try and update.
            • 3. Re: How to capture frame maximizing/minimizing event?
              918178
              Hi, I confused a little bit. I did not mean minimized but restored. Please excuse.
              • 4. Re: How to capture frame maximizing/minimizing event?
                918178
                hi,

                I used frame component listener to capture the maximizing/restoring event. But when I tried to set preferred size for table it is not all setting it dynamically when maximized/restored.

                Please give some support on this.

                My code

                'f' the frame instance. The code pass thro' the if loop but the preferred size is not setting when maximized.

                f.addComponentListener(new ComponentListener() {
                               public void componentResized(ComponentEvent e) {
                                    if(f.getExtendedState()>=6){
                                         System.out.println("Maximized");
                                    table.getTablePane().setPreferredSize(new Dimension(1250, 450));
                                    }
                               }

                               public void componentHidden(ComponentEvent arg0) {
                               }

                               public void componentMoved(ComponentEvent arg0) {
                               }

                               public void componentShown(ComponentEvent arg0) {
                               }
                               });
                • 5. Re: How to capture frame maximizing/minimizing event?
                  Darryl Burke
                  Moved from Java Programming. Please take the trouble to find an appropriate section before you start a thread.

                  Also, announcements at the top of the forum listings are meant to be read. Vlearly, you didn't bother.

                  db
                  • 6. Re: How to capture frame maximizing/minimizing event?
                    Darryl Burke
                    Your approach is fundamentally flawed. Learn to use layout managers correctly; don't try to set the preferred size.
                    http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html

                    db
                    • 7. Re: How to capture frame maximizing/minimizing event?
                      918178
                      I cannot see any specific section to post swing related queries. I found this section/New To Java section nearly relevant. That s y posted here.
                      • 8. Re: How to capture frame maximizing/minimizing event?
                        918178
                        Hi,

                        I have coded my requirement as below:


                        mainFrame.addComponentListener(new ComponentListener() {                                           // frame where panel->scroll panel is fixed
                                       public void componentResized(ComponentEvent e) {         // captured the events by passing component listener
                                            if (mainFrame.getExtendedState() == 6) {      // captures the window maximizing event
                                                 table.getTablePane().setPreferredSize(
                                                           new Dimension(1250, 450)); // set required size
                                                 table.getTablePane().revalidate(); // revalidated.
                                            } else {                                                                               // captures window restoring event
                                                 table.getTablePane().setPreferredSize(
                                                           new Dimension(715, 450));
                                                 table.getTablePane().revalidate();
                                            }
                                       }
                                       public void componentHidden(ComponentEvent arg0) {}

                                       public void componentMoved(ComponentEvent arg0) {}

                                       public void componentShown(ComponentEvent arg0) {}
                                  });

                        Thanks for everyone who helped me.