0

I have an application in Java with 2 JPanels inside a LayeredPane inside a JFrame. I'd like that when I resize window frame, all panels are resized althougth its components. Here is my code for the resized function:

 mainFrame.addComponentListener(new ComponentListener() {

        @Override
        public void componentShown(ComponentEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void componentResized(ComponentEvent e) {
            // Resize components of JFrame to fit new size
            int width = e.getComponent().getWidth();
            int height = e.getComponent().getHeight();

            if(containerJPanel != null) {
                containerJPanel.setBounds(0, 0, 
                        width-DEFAULT_PANEL_MINUS_WITDH, height-DEFAULT_PANEL_MINUS_HEIGHT);
                containerJPanel.setPreferredSize(new Dimension(width-DEFAULT_PANEL_MINUS_WITDH, height-DEFAULT_PANEL_MINUS_HEIGHT));

            }


            if(mainJPanel != null) {
                mainJPanel.setBounds(0, 0, 
                        width-DEFAULT_PANEL_MINUS_WITDH, height-DEFAULT_PANEL_MINUS_HEIGHT);
                mainJPanel.setPreferredSize(new Dimension(width-DEFAULT_PANEL_MINUS_WITDH, height-DEFAULT_PANEL_MINUS_HEIGHT));
            }


            if(loadingPanel != null) {
                loadingPanel.setBounds(0, 0, 
                        width-DEFAULT_PANEL_MINUS_WITDH, height-DEFAULT_PANEL_MINUS_HEIGHT);
                loadingPanel.setPreferredSize(new Dimension(width-DEFAULT_PANEL_MINUS_WITDH, height-DEFAULT_PANEL_MINUS_HEIGHT));
            }

        }

        @Override
        public void componentMoved(ComponentEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void componentHidden(ComponentEvent e) {
            // TODO Auto-generated method stub

        }
    });

Here is the variable definitions:

 /** Main frame **/

 mainFrame = new JFrame();
 mainFrame.setBounds(100,100,DEFAULT_WINDOW_WIDTH,DEFAULT_WINDOW_HEIGHT);
 mainFrame.setPreferredSize(new Dimension(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT));
 mainFrame.getContentPane().setLayout(new BorderLayout(0, 0));
 containerJPanel = new JLayeredPane();


 mainFrame.getContentPane().add(containerJPanel, BorderLayout.CENTER);
 containerJPanel.setBounds(0,0,
                    DEFAULT_WINDOW_WIDTH-DEFAULT_PANEL_MINUS_WITDH, DEFAULT_WINDOW_HEIGHT-DEFAULT_PANEL_MINUS_HEIGHT);


 /** Main Panel **/
 mainJPanel = new JPanel();
 mainJPanel.setBounds(0,0,
                    DEFAULT_WINDOW_WIDTH-DEFAULT_PANEL_MINUS_WITDH, DEFAULT_WINDOW_HEIGHT-DEFAULT_PANEL_MINUS_HEIGHT);
 containerJPanel.add(mainJPanel, new Integer(0), 0);
 /** Loading Panel **/
 loadingPanel = new JPanel();
 containerJPanel.add(loadingPanel, new Integer(1), 0);

 loadingPanel.setBounds(0,0,
                    DEFAULT_WINDOW_WIDTH-DEFAULT_PANEL_MINUS_WITDH, DEFAULT_WINDOW_HEIGHT-DEFAULT_PANEL_MINUS_HEIGHT);
 loadingPanel.setLayout(new BorderLayout(0, 0));
 loadingPanel.setOpaque(false);

The point is to have two panels so one is visible at a time. I'm sure I'm doing something wrong, but I can't realise it! Thanks for the help.

Nebuchanazer
  • 121
  • 1
  • 1
  • 11
  • 3
    Do you really want a JLayeredPane()? A typical usecase for showing one Panel at a time would be a Panel with CardLayout... – JensS Sep 06 '17 at 20:53
  • `JLayerePane` has a `null` layout by default, have you considered changing the layout manager to something else which would then be capable of resizing the components in response to a size change in the window – MadProgrammer Sep 06 '17 at 21:46
  • 1
    See an [example](https://stackoverflow.com/a/46013230/3992939) of using CardLayout. What are you trying to achieve ? – c0der Sep 07 '17 at 04:21
  • Thank you all. I'm gonna try the CardLayout! I'll be back with my comments. – Nebuchanazer Sep 07 '17 at 11:07
  • Perfect! That works. Thank you all. – Nebuchanazer Sep 08 '17 at 18:23

2 Answers2

0

As c0der said: see the example of using CardLayout.

Nebuchanazer
  • 121
  • 1
  • 1
  • 11
0

I have a poor but working solution : in my actionPerformed which is timed (by a javax.swing.Timer.Timer) every -time Delay In ms-, I use this.remove(panelToRefresh) change the bounds to the new window bounds and this.add(panelToRefresh) this costs a bit to your computer but surprisingly don't flicker to much the timer

gameTime = new Timer(120, this);
gameTime.start();
minimal code in ActionPerformed

this.remove(backGround);
backGround.setSize(this.getWidth(), this.getHeight() + getInsets().top - getInsets().bottom);
this.add(backGround);
Victor
  • 1
  • 3