8 Replies Latest reply: Mar 2, 2012 4:21 AM by 863331 RSS

    Resizing JFrame Window messes up the contents of the window

    user13731090
      Hello All,

      I have a Jaframe Window and the content of this window are 2 panels,
      1. JPanel which contains another panel and is using GridBagLayout
      2. JPanel which is a status panel

      so my code could be summerized to..

      JPanel mainPanel = new JPanel(GridBagLayout());
      JPanel dataPanel = new JPanel();
      mainPanel.add(DataPanel, c);

      JPanel statusPanel = new JPanel()

      frame.getContentPane.add(mainPanel, BorderLayout.CENTER);
      frame.getContentPane.add(statusPanel, BorderLayout.SOUTH);

      Everything works fine,
      I have not deliberately added a scrollpane to the window, because i dont want to see the scroll bars, that is a requirement for the application, user does not want scroll bars.
      When the window resizes, i need to scale my data panel and it is working fine.

      But the problem is, when i click on the window border, for resizing the window, my data panel get shifted so that i see only top left portion of the data panel.
      But after i am done with resizing the window, i have placed code to put the data panel in the center of the window and that is working fine.

      But the problem is when i click on the window border to resize, it messes up the data panel, shifts it and it is not even visible completely.
      I read about continuous layout and tried to turn it off, but Window resize gets messy when window sizze is bigger than my content panel.

      Please advice what do i need to do here inorder to stop the contents of my data panel shifting to some place when i click ont he window border, inorder to resize the window.

      Any help is appreciated.
      Thanks
        • 1. Re: Resizing JFrame Window messes up the contents of the window
          863331
          Hi,

          can you post some more code for us to see where the real problem is? My bet go to GridBagLayout issue (not a problem, just a side effect)


          RGV

          Edited by: ruivale on Mar 1, 2012 2:52 PM
          • 2. Re: Resizing JFrame Window messes up the contents of the window
            919944
            Did you set border layout for the frame? It's hard to figure out without exact version of your code.
            • 3. Re: Resizing JFrame Window messes up the contents of the window
              user13731090
              Thanks for the reply,
              i am aftraid i can post the whole code since it is a huge file.

              Yes, i do use Border Layout for the frame's content pane.

              Here are the related code snippet.
              in the jframe class this is the related code snippet:

              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
                   setTitle(title);
                   mainPanel.setBackground(new java.awt.Color(0, 0, 0));
                   
                   
                   getContentPane().add(mainPanel, BorderLayout.CENTER);
                   
                   getContentPane().add(statusPanel, BorderLayout.SOUTH);

              mainPanel.add(dataPanel);



              (i have tried setting some constraint for GridBag Layout to see if that was the issue like setting the fill and anchor, but same thing happens, when i click on the border of the window to resize the data panel gets dispositioned to some other palce.)

              Here is my code for tracking window resize

              m_mainWindow.addComponentListener(new ComponentAdapter(){
                   public void componentResized(ComponentEvent e) {
                             
                        int width = m_mainWindow.getWidth();
                        int height = m_mainWindow.getHeight();
                             
                             
                        
                        if(width <= 0 || height <= 0) return;
                                            
                        DataPanel dp = getDataPanel();
                        
                        
                        int dataWidth = dp.getPrefWidth();
                        int dataHeight = dp.getPrefHeight();
                        if(dataWidth <= 0 || dataWidth <= 0) return;
                        
                        
                        double hScale = width * 1.0 / dataWidth;
                        double vScale = height * 1.0 / dataHeight;

                        dp.setScale(hScale, vScale);
                        
                                  
                        m_mainWindow.invalidate();
                        
                        
                        m_mainWindow.getContentPane().getComponent(0).invalidate();
                        dp.invalidate();
                   }
                        
                   });



              Please let me know if i am doing somethig wrong here.

              thanks
              • 4. Re: Resizing JFrame Window messes up the contents of the window
                863331
                Hi again ... it's better, but not perfect! ;-)

                What setScale(..) method, from DataPanel, do?


                RGV
                • 5. Re: Resizing JFrame Window messes up the contents of the window
                  user13731090
                  Hello,
                  thanks again,

                  setScale method, reduces the size of the dataPanel, by scale,
                  to be precise, here is the code:

                  data panel has required height and width fields and it is multiplying that with scale and setting that to the prefered size so that my data panel reduces in size.
                  Data Panel has all custom drawing and that code is actually managing the setScale pretty well. When scale is reduced, everything is shrinking fine.

                  setScale(double hscale, double vScale){
                  int newWid = idealWid * hScale;
                  int newHeight = idealHeight * vScale;

                  setPreferredSize(new Dimension(newWid, newHeight));
                  }

                  IS there something that i can do to stop the dataPanel shifting when i click ont he Window border for resizing?

                  Thanks
                  • 6. Re: Resizing JFrame Window messes up the contents of the window
                    863331
                    Well, I'm finding it difficult to see the problem .. :-( could be me!

                    I'm still betting the "guilt" does to GridBagLayout! OK, you've already figure out that i'm no fan of GridBagLayout!

                    If you REALLY resize the window, when you let the mouse all the components will be OK, right? The problem is only when you click the border ...
                    Does your window resize listener is fired when you only select the window border?
                    Can you quicky change your GridBagLayout for other layout, like BorderLayout for instance?

                    But if you want to "kill" yout problem and answering your question "IS there something that i can do to stop the dataPanel shifting when i click ont he Window border for resizing?" I would try to add a empty JPanel to the window content pane while resizing, and only when the win resize is done, i would add the "real" contents to the window. Of course you will loose the continuous layout while resizing ...

                    I sorry if i cannot help you more ... :-(


                    RGV
                    • 7. Re: Resizing JFrame Window messes up the contents of the window
                      user13731090
                      I wont get any notifications when user clicks on the border for resizing hence i wont be able to put an empty panel when user tries to resize
                      • 8. Re: Resizing JFrame Window messes up the contents of the window
                        863331
                        Hi again,

                        It would be easier if you post your real code ... this way is harder to help you!


                        RGV