Search code examples
javaswinguser-interfacelayout-managergridbaglayout

GridBagLayout not applying gridy changes


I'm trying to create a personal DnD character sheet program. The main idea is to have 4 large panels that each contain one of the major sections of a basic character sheet. I'm currently working on the first panel that has stats and saving throws. I'm trying to get more comfortable with GridBagLayout while making this, but I've run into a problem with setting gridy. I've already visited the GridBagLayout not obeying gridx and gridy and (unless I'm just stupid), that didn't help me. I used GridBagLayout with GridBagConstraints for statsPanel() and gridy worked fine.

Here's the problem: whenever I set gridy to the next grid in proficinciesAndSkillsPanel(), it's treated like I just changed gridx. My goal is to have one column with many rows, not one row with many columns. Thank you for your time

//this builds the jframe and sets the primary jpanel
private void buildComponents()
{
    setSize(800, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel mp = (JPanel)getContentPane();
    mp.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    gbc.gridx = 0;
    gbc.gridy = 0;

    mp.add(panelA(), gbc);



    gbc.gridx = 1;

    mp.add(new JButton("test"), gbc);

    createMenuBar();
    setVisible(true);
}

//this creates the first real panel that i'm currently working with
private JPanel panelA()
{
    JPanel result = new JPanel();
    result.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.fill = GridBagConstraints.BOTH;

    gbc.gridx = 0;
    gbc.gridy = 0;

//I left out the code for statsPanel() because that works fine
    result.add(statsPanel(), gbc);

    gbc.gridx = 1;

    result.add(proficinciesAndSkillsPanel(), gbc);

    return result;
}

//this builds the second half of the upper portion of panel A
private JPanel proficinciesAndSkillsPanel()
{
    JPanel result = new JPanel();
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = 1;
    gbc.weighty = 1;

    result.add(labeledTextField("Inspiration", inspirationField = new JTextField(2), null, 1), gbc);

    gbc.gridx = 0;
    gbc.gridy = 1;

    result.add(labeledTextField("Proficiency bonus", proficiencyField = new JTextField(2), null, 1),gbc);

    gbc.gridx = 0;
    gbc.gridy = 2;

    result.add(labeledRadioField("Strength", strSTField = new JTextField(2), strRB = new JRadioButton()),gbc);

    return result;
}

//this creates a JTextField with the appropriate label and a sub-JTextField
private JPanel labeledTextField(String str, JTextField jtf, JTextField bonjtf, int space)
{
    JPanel result = new JPanel();
    JPanel subResult = new JPanel();
    result.setLayout(new FlowLayout());
    result.add(new JLabel(str));
    result.add(Box.createHorizontalStrut(space));
    subResult.add(jtf);
    jtf.setHorizontalAlignment(JTextField.CENTER);
    try
    {
        subResult.add(bonjtf);
        bonjtf.setHorizontalAlignment(JTextField.CENTER);
        bonjtf.setEditable(false);
        bonjtf.setText("+0");
    }catch(NullPointerException e){}
    jtf.addKeyListener(new JTF_Listener(){
        public void update() {
            String str2 = "";
            try
            {
                int result = (Integer.parseInt(jtf.getText())-10)/2;
                if(result >=0)
                {
                    str2 += "+"+Integer.toString(result);
                }
                else
                {
                    str2 += Integer.toString(result);
                }
            }catch(NumberFormatException nfe){}
            bonjtf.setText(str2);
        }
    });
    result.add(subResult);
    return result;
}

//this does the same as labeledTextField, just with a radioButton
private JPanel labeledRadioField(String str, JTextField jtf, JRadioButton jrb)
{
    JPanel result = new JPanel();
    result.setLayout(new FlowLayout());
    result.add(jrb);
    result.add(jtf);
    result.add(new JLabel(str));
    jtf.setHorizontalAlignment(JTextField.CENTER);
    jtf.setText("+0");
    jtf.addKeyListener(new JTF_Listener(){
        public void update(){
            String str2 = "";
            try
            {
                int result = Integer.parseInt(jtf.getText());
                str2+= "+" + Integer.toString(result);
            }catch(NumberFormatException nfe){}
            jtf.setText(str2);
        }
    });

    return result;
}

Solution

  • Not sure that this is your problem since you've not posted a valid MCVE (please correct this!), but here:

    private JPanel proficinciesAndSkillsPanel()
    {
        JPanel result = new JPanel();  // ******** here *********
        GridBagConstraints gbc = new GridBagConstraints();
    
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.weightx = 1;
        gbc.weighty = 1;
    
        result.add(labeledTextField("Inspiration", inspirationField = new JTextField(2), null, 1), gbc);
    
        gbc.gridx = 0;
        gbc.gridy = 1;
    
        result.add(labeledTextField("Proficiency bonus", proficiencyField = new JTextField(2), null, 1),gbc);
    
        gbc.gridx = 0;
        gbc.gridy = 2;
    
        result.add(labeledRadioField("Strength", strSTField = new JTextField(2), strRB = new JRadioButton()),gbc);
    
        return result;
    }
    

    You're treating this result JPanel as if it uses GridBagLayout when in fact it's not, it's using JPanel's default FlowLayout

    One confusing bit: you've got many JPanel variables that have been given the same name, result. In your code you do in fact call result.setLayout(new GridBagLayout()), but not for the JPanel that I show above, and this might be confusing you. I suggest that you avoid using the same variable names in your code as you're doing to avoid this confusion.

    If you need more specific help, then you first, please tell us more of the details and show us your pertinent code as a valid minimal example program or MCVE. If you're sitting in our shoes, and are trying to understand someone else's confusing code, it makes a huge difference if they put the effort in to make that code compilable and runnable for us.