Search code examples
javauser-interfacelayoutjspinner

My Frames take up most of the space availbe


So I have a problem that I have to make a questionnaire about something and I have to use multiple Layouts.

My problem is that when I add 2 JPanels to a Grid layout (only to 1 side) my first Panel takes up most of the space.

Code:

public class MainFrame extends JFrame implements ItemListener{
        JPanel mainPanel,rightSideAge,rightSideGender,leftSide,rightSideBox,leftSideBox;
        JTextArea nameArea;
        JSpinner ageSpinner;
        JRadioButton genMale,genFema;
        ButtonGroup genderGroup;

    MainFrame(){
        this.setSize(1000, 800);
        this.setLocationRelativeTo(null);
        this.setTitle("Közvélemény kutatás a zenei ízlésekről");
        mainPanel = new JPanel(new GridLayout(0, 2));
        this.setContentPane(mainPanel);

        /* --- RIGHT PANEL --- */
        rightSideBox = new JPanel();
        rightSideBox.setLayout(new BoxLayout(rightSideBox, BoxLayout.Y_AXIS));

        rightSideAge = new JPanel(new FlowLayout(FlowLayout.LEFT));
        rightSideAge.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        //rightSide.setLayout(new BoxLayout(rightSide, BoxLayout.Y_AXIS));
        mainPanel.add(rightSideBox);

            //Age label
        //JLabel labelAge = new JLabel("Kor: ");
        //labelAge.setSize(100, 30);
            //Age Spinner
        ageSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 120, 1));
        ageSpinner.setPreferredSize(new Dimension(40, 20));
        Component mySpinnerEditor = ageSpinner.getEditor();
        JFormattedTextField jftf = ((JSpinner.DefaultEditor) mySpinnerEditor).getTextField();
        jftf.setColumns(5);

            //New box for zenei ízlés
        rightSideGender = new JPanel();
        rightSideGender.setBorder(BorderFactory.createLineBorder(Color.GREEN));
        rightSideGender.setLayout(new BoxLayout(rightSideGender,BoxLayout.Y_AXIS));

            //Gender ComboBox
            genderGroup = new ButtonGroup();
            genMale = new JRadioButton("Férfi");
            genderGroup.add(genMale);
            genFema = new JRadioButton("Nő");
            genderGroup.add(genFema);

        /* --- LEFT SIDE --- */

        rightSideBox.setBorder(BorderFactory.createLineBorder(Color.RED));


        /* ADD STUFF TO PANELS */
            /* RightSideBox */
            rightSideBox.add(rightSideAge);
            rightSideBox.add(rightSideGender);
                /*RIGHT SIDE PANELS*/
                rightSideGender.add(new JLabel("Nem:"));
                rightSideGender.add(genMale);
                rightSideGender.add(genFema);
                rightSideAge.add(new JLabel("Kor"));
                //rightSide.add(labelAge);
                rightSideAge.add(ageSpinner);
                /*LEFT SIDE PANEL*/

        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

The Blue lineout sould be only under the JSpinner:

https://i.sstatic.net/xF5At.png


Solution

  • ageSpinner.setPreferredSize(new Dimension(40, 20));
    

    First of all, you should not be manually setting the size of a component. Each Swing component is responsible for determining its own size.

    The Blue lineout should be only under the JSpinner:

    The box layout will resize a component to its maximum size if there is space available. For some reason a JSpinner doesn't appear to have a maximum height so it expands to fill all the available space.

    To fix this you can do something like:

    //ageSpinner.setPreferredSize(new Dimension(40, 20));
    ageSpinner.setMaximumSize( ageSpinner.getPreferredSize() );