1

i am trying to design a JFrame with 2 different JPanels in it, one on the left with AbsoluteLayout and one on the right with a GridLayout with variable dimensions.

After adding some components to the JPanels, i add them to the JFrame contentPane and use the method JFrame.pack() hoping to get a JFrame with the minimum size possible that can show all of its components, but what i am getting is the minimum size to show only the JPanel on the right with the GridLayout, the JPanel on the left gets overlapped by the one on the right.

Is there any good way to use the JFrame.pack() method and it still shows both JPanel completly?

Here is the code:

public class GameGUI extends JFrame{

   private int labSize;
   private JFrame mainFrame;
   private JPanel labPanel;
   private JPanel choicesPanel;
   private JButton exitButton;
   private JButton replayButton;

   public GameGUI(int n) {

        labSize=n;    
        mainFrame = new JFrame("Maze Game");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.getContentPane().setLayout(new GridLayout(1, 0));
        mainFrame.setVisible(false);    
        labPanel=new JPanel(new GridLayout(labSize,labSize));    
        choicesPanel=new JPanel(new GridLayout(0, 1));
        choicesPanel.setLayout(null);           
        replayButton=new JButton("Replay");
        replayButton.setBounds(10, 11, 80, 30);         
        exitButton=new JButton("Exit");
        exitButton.setBounds(10, 51, 80, 30);    
        choicesPanel.add(replayButton);
        choicesPanel.add(exitButton);           
        mainFrame.getContentPane().add(choicesPanel);
        mainFrame.getContentPane().add(labPanel);
    }

    public void refreshLabShowing(char[][] lab){

        labPanel.removeAll();    
        for(int i=0;i<labSize;i++){
            for(int u=0;u<labSize;u++){
                labPanel.add(new JLabel(String.valueOf(lab[i][u])));
            }
        }           
        mainFrame.pack();
        mainFrame.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Don't use `null` layout. That's the source of the problem (and will likely be a source of more problems later). The panel with the `null` layout does not have a reasonable preferred size, because calculating that is usually the layout manager's job. You could also override `getPreferredSize()`, but in this case you should not as there's no good reason to not use a layout manager. – kiheru Mar 31 '15 at 13:36
  • Note that the two absolutely positioned buttons can easily be laid out using a single column `GridLayout` and an `EmptyBorder`. To ensure that they only occupy as mush space as required, add the (panel with) grid layout to another panel with `FlowLayout`. It's a lot easier than trying to work around the innumerable problems that attempts at absolute positioning will cause. – Andrew Thompson Mar 31 '15 at 15:39

1 Answers1

2

pack() will only work if with layout managers, as pack() itself queries layout manager for required dimension so here, you cannot use absolute layout and pack().

Antoniossss
  • 31,590
  • 6
  • 57
  • 99