Search code examples
javaswinglayoutlayout-managergridbaglayout

GridBagLayout doesn't set JPanel position as expected


So I'm originally using a BorderLayout for the mainscreen, but once the user hits start, it converts to a GridBagLayout with the intention of being 80% game screen on the left and 20% menu, score, etc on the right.

| --- GameScreen --- | Score |

However I'm having difficulty getting this as either I can't resize the score screen or it doesn't print out correctly at all. This is the code I'm currently using.

public Launcher(String title) {
    super(title);
    this.setResizable(false);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    cp = getContentPane();
    // cp.setSize((int)(1920*0.5), (int)(1080*0.5));
    cp.setLayout(new BorderLayout());
    panel = new JPanel();
    panel.setBackground(Color.YELLOW);
    // panel.setSize(this.getWidth()/4, this.getHeight());
    panel.add(startButton);
    panel.add(pauseButton);
    panel.add(quitButton);
    setSize(1920, 1100);
    cp.add(panel, BorderLayout.SOUTH);
    this.getContentPane().setBackground(Color.GREEN);
    //ActionListenerStuff for buttons
    .
    .
    .

This is the ACtionListenerMethod that calls the updateGui method.

    else if (e.getSource() == startButton) {

                cp.remove(panel);

                panel.setSize(1200, getHeight());

                state = STATE.RUNNING;
                updateState();
                updateGui();
                createGameLoop(); 
    }

This is the method where I change the layout and add the JPanels.

    private void updateGui() {
    cp.setLayout(new GridBagLayout());
    GridBagConstraints layoutConstraints = new GridBagConstraints();
    layoutConstraints.fill = GridBagConstraints.VERTICAL;
    layoutConstraints.weighty = 1;
    layoutConstraints.weightx = 0.8;
    cp.add(house, layoutConstraints);

    layoutConstraints.weighty = 1;
    layoutConstraints.weightx = 0.2;
    cp.add(panel, layoutConstraints);
}

It ends up with the 'house' JPanel being 80% but the 'panel' variable is still on the border of the bottom from the previous borderlayout. Not sure why though.

I'm not entirely sure if that's enough information and will gladly post whatever information that's required.

Edit: Tried adding layoutConstraints.gridy = 1; but that didn't work either.

private void updateGui() {
    cp.setLayout(new GridBagLayout());
    GridBagConstraints layoutConstraints = new GridBagConstraints();
    layoutConstraints.fill = GridBagConstraints.VERTICAL;
    layoutConstraints.weighty = 1;
    layoutConstraints.weightx = 0.8;
    house.setMinimumSize(new Dimension(1600, 1080));
    cp.add(house, layoutConstraints);

    GridBagConstraints layoutConstraints2 = new GridBagConstraints();
    layoutConstraints2.fill = GridBagConstraints.VERTICAL;
    layoutConstraints2.weighty = 1;
    layoutConstraints2.weightx = 0.2;
    layoutConstraints.gridy = 1;
    cp.add(panel, layoutConstraints2);
}

private void updateState() {
    if (roomGenerator == null) {
        roomGenerator = new RoomGenerator();
    }
    roomGenerator.updateRoom();

    Room room = roomGenerator.getRoom();
    house = (House) roomGenerator.getRoom();
    house.setSize(300, getHeight());
    this.getContentPane().add(house, BorderLayout.CENTER);

    startButton.setVisible(false);

    // this plugin puts the buttons in the House JPanel
    // house.add(pauseButton);
    // house.add(quitButton);
}

This is what I currently have. This is what's displaying. The drawing should be the "80%" portion and the yellow should be the 20%.

Edit 2: Using Camickr's changes I end up with this.

It's a step in the right direction, but it's not setting the size of the house JPanel correctly as far as I can tell.

private void updateGui() {

    cp.add(panel, BorderLayout.LINE_END);
    panel.setVisible(true);
    cp.revalidate();

}

private void updateState() {
    if (roomGenerator == null) {
        roomGenerator = new RoomGenerator();
    }
    roomGenerator.updateRoom();

    Room room = roomGenerator.getRoom();
    house = (House) roomGenerator.getRoom();
    house.setSize(1500, getHeight());
    this.getContentPane().add(house, BorderLayout.CENTER);

    startButton.setVisible(false);

}

Solution

  • If anyone was curious I figured it out. I did it via this way...

        cp.setLayout(new GridBagLayout());
        GridBagConstraints layoutConstraints = new GridBagConstraints();
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.gridx = 0;
        layoutConstraints.gridy = 0;
        layoutConstraints.weighty = 1;
        layoutConstraints.weightx = 0.8;
        house.setMinimumSize(new Dimension(1500, 1080));
        cp.add(house, layoutConstraints);
    
        GridBagConstraints layoutConstraints2 = new GridBagConstraints();
        layoutConstraints2.fill = GridBagConstraints.BOTH;
        layoutConstraints2.weighty = 1;
        layoutConstraints2.weightx = 0.2;
        layoutConstraints2.gridx = 1;
        layoutConstraints2.gridy = 0;
        cp.add(dataPanel, layoutConstraints2);
    
        dataPanel.setVisible(true);
        cp.revalidate();