H4rdstyler
H4rdstyler

Reputation: 71

Dynamicly adding JPanels to a JFrame

This is my first question inhere, so please bear with me :) Im working on a dynamic part of a GUI, and for some reason its teasing me. The class is opened in a new window after login. What I want, is for a new JPanel to be built and added to 'container' every time the "add player" button is clicked. It is supposed to put them below eachother, yet all it does is to add one button on the first click, and then the rest of the clicks afterwards does nothing.

Any help is appreciated :)

package Gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
//import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class TeamManagerGUI extends JFrame implements ActionListener
{
//  private JLabel pNameLabel = new JLabel("Player Name: "),
//                  pSchoolLabel = new JLabel("School: ");
//  private JTextField pNameField = new JTextField(),
//                      pSchoolField = new JTextField();
    private JButton addButton = new JButton("Add Player"),
                    removeButton = new JButton("Remove player");
    private JPanel container = new JPanel(),
                    playerContainer = new JPanel();


    int frameCounter = 1;

    public TeamManagerGUI()
    {
        super("Team Manager User Interface");
        setSize(1200,800);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        container.setLayout(new GridBagLayout());
        playerContainer.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(3,3,3,3);
        addButton.addActionListener(this);
        container.add(addButton,gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(3,3,3,3);
        container.add(removeButton,gbc);

        this.add(container);
    }

    public void playerFrame()
    {
        GridBagConstraints gbc = new GridBagConstraints();

        playerFrameArr.add(new JPanel());

        gbc.gridx = 0;
        gbc.gridy = 0;
        playerContainer.add(new JButton("LABEL"),gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        playerContainer.add(new JButton("BUTTON"),gbc);

        gbc.gridx = 0;
        gbc.gridy = frameCounter+1;
        container.add(playerContainer,gbc);

        System.out.println(frameCounter);

        frameCounter++;
    }

    public void addPlayerRow()
    {
        playerFrame();
        container.revalidate();
        container.repaint();
    }

    public void removePlayerRow()
    {
        //Not yet implemented
    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource() == addButton)
        {
            addPlayerRow();
        }
        if(ae.getSource() == removeButton)
        {
            //Not yet implemented
        }
    }
}

Upvotes: 0

Views: 57

Answers (1)

Nitram
Nitram

Reputation: 6716

You are adding the playerContainer again and again. I think you should actually use the newly created JPanel. This one should be populated and added to the main container.

Adding a single panel multiple times will not render properly as this screws up the layout. I think you need to keep a reference to the new JPanel and fill this one with your layout and the buttons.

I am thinking something like this:

public void playerFrame()
{
    GridBagConstraints gbc = new GridBagConstraints();

    JPanel newPanel = new JPanel(new GridBagLayout());
    playerFrameArr.add(newPanel);

    gbc.gridx = 0;
    gbc.gridy = 0;
    newPanel.add(new JButton("LABEL"), gbc);

    gbc.gridx = 1;
    gbc.gridy = 0;
    newPanel.add(new JButton("BUTTON"), gbc);

    gbc.gridx = 0;
    gbc.gridy = frameCounter + 1;
    container.add(newPanel, gbc);

    System.out.println(frameCounter);

    frameCounter++;
}

Upvotes: 1

Related Questions