JTextField not fillling up column as expected?

58 Views Asked by At

Learning to use MigLayout. I am trying to make a very simple form that is two columns, a label, a text input, repeat, until a submit button on the bottom that should span both columns. However it does not seem to be working as expected. The text fields are not spreading and neither is the button. It looks like this enter image description here

Here is my code -

package com.mypackage;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import net.miginfocom.swing.MigLayout;

public class DiceOddCalculator extends JFrame {
    public DiceOddCalculator(String title) {
        this.setSize(500, 500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setTitle(title);

        //Layout and panel to house components
        MigLayout layout = new MigLayout("wrap 2, debug");
        JPanel panel = new JPanel(layout);

        //Components
        JLabel maxDiceLabel = new JLabel("Max value of die");
        JTextField maxDiceInput = new JTextField();
        JLabel protectionCriteriaLabel = new JLabel("Die protection Criteria:");
        JTextField protectionCriteriaInput = new JTextField("");
        JLabel numOfDiceLabel = new JLabel("Number of dice:");
        JTextField numOfDiceInput = new JTextField("");
        JButton addDice = new JButton("Add dice");

        //add to panel
        panel.add(maxDiceLabel);
        panel.add(maxDiceInput);
        panel.add(protectionCriteriaLabel);
        panel.add(protectionCriteriaInput);
        panel.add(numOfDiceLabel);
        panel.add(numOfDiceInput);
        panel.add(addDice, "span");

        //Turn window on
        this.setContentPane(panel);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        DiceOddCalculator f = new DiceOddCalculator("Dice Odds Simulator");
    }
}

What needs to change in my code so that the JTextField and Button are the appropriate widths?

1

There are 1 best solutions below

0
bjk116 On

I figured out it had to do with how I was constructing MigLayout. Using MigLayout layout = new MigLayout("wrap 2, debug", "[fill, grow]", "") made the component stretch across its column as I was expecting.