Hello Lovely People,

Please have a look at my GUI first, before reading the rest (I've attached a picture, see at the end of my post).

I'm having two problem with this GUI.

1) I don't want my Start button to be as long as it is. I want it to be about the half of if its current size and at the centre (as it is). how do I fix this?

2)As you can see, my text fields are quiet big as well. I would like to shorter both, in height and length (it should have enough room for 4 digits).

Here's My Code, Hope someone can help.
Thanks In Advance:)

package prjectilegui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


class ControlPanel extends JPanel
{
    //Fields
    private JLabel initialHeight, initialVelocity, initialAngle,empty1,empty2,empty3,empty4;
    public JRadioButton position, velocity, acceleration, energy;
    private ButtonGroup bg;
    public JTextField heightTextField, velocityTextField, angleTextField;
    private JButton start;
    private JPanel higherPanel, centerPanel, lowerPanel;

    //Default Constructor
    public ControlPanel()
    {
        setLayout(new GridLayout(3,1));

        // initializing Start Button
        start = new JButton("Start");
        start.addActionListener(new startButtonListener());

        // creating empty space to correctly place the start button
        empty3 = new JLabel("");
        empty4 = new JLabel("");
        
        // creating higher panel
        higherPanel = new JPanel(new GridLayout(3,1));
        higherPanel.add(empty3);
        higherPanel.add(start);
        higherPanel.add(empty4);

        // creating center and lower panels
        createCenterPanel();
        createLowerPanel();

        // adding panels to ControlPanel
        add(higherPanel);
        add(centerPanel);
        add(lowerPanel);
    }

    private void createCenterPanel()
    {
        // center panel
        centerPanel = new JPanel(new GridLayout(3,2));

        // initializing labels
        initialHeight = new JLabel("Initial Height     ");
        initialVelocity = new JLabel("Initial Velocity     ");
        initialAngle = new JLabel("Initial Angle     ");

        // initializing textfields
        heightTextField = new JTextField(2);
        velocityTextField = new JTextField(2);
        angleTextField = new JTextField(2);

        // adding elements to centerPanel
        centerPanel.add(initialHeight);
        centerPanel.add(heightTextField);
        centerPanel.add(initialVelocity);
        centerPanel.add(velocityTextField);
        centerPanel.add(initialAngle);
        centerPanel.add(angleTextField);
    }

    private void createLowerPanel()
    {
        lowerPanel = new JPanel(new GridLayout(6,1));

        // initializing radiobuttons
        position = new JRadioButton("Position");
        velocity = new JRadioButton("Velocity");
        acceleration = new JRadioButton("Acceleration");
        energy = new JRadioButton("Energy");

        // creating empty space
        empty1 = new JLabel("");
        empty2 = new JLabel("");
        // initializing buttongroup and add radiobutton to it
        bg = new ButtonGroup();
        bg.add(position);
        bg.add(velocity);
        bg.add(acceleration);
        bg.add(energy);

        // add radioButtons and empty space to lowerPanel
        lowerPanel.add(empty1);
        lowerPanel.add(position);
        lowerPanel.add(velocity);
        lowerPanel.add(acceleration);
        lowerPanel.add(energy);
    }

    //Start button listener
    private class startButtonListener implements ActionListener
    {

        public void actionPerformed(ActionEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

    }
}

Dani AI

Generated

The long Start button and oversized text fields both come from using GridLayout for the parent panels. GridLayout forces each cell to the same size and stretches children to fill the cell, so the middle row (where the Start button lives) and the center row (text fields) expand to fill available space. ’s empty-label trick works for positioning but is the root cause of the sizing problems. is right that GridBagLayout gives precise control, and ’s pointer to the layout docs is useful — but two smaller changes give the fix with minimal learning.

Make the top row use a layout that respects component preferred sizes (no empty labels needed). Example:

JPanel higherPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
higherPanel.setBorder(BorderFactory.createEmptyBorder(8, 0, 8, 0));
higherPanel.add(start);

For the input rows, avoid placing text fields directly into a GridLayout cell. Use small row panels (FlowLayout) or BoxLayout vertically, and set the field columns (or preferred/max size) to keep them compact for four digits:

JTextField heightField = new JTextField(4);            // 4 columns = roughly 4 digits
heightField.setMaximumSize(heightField.getPreferredSize());

JPanel row = new JPanel(new FlowLayout(FlowLayout.LEFT, 6, 0));
row.add(new JLabel("Initial Height"));
row.add(heightField);
centerPanel.add(row);   // centerPanel can use BoxLayout.Y_AXIS

Other tips: prefer pack() on the top-level window so preferred sizes are honored; avoid hard-coded pixel heights unless necessary; use Insets/EmptyBorder or struts for spacing. If the layout must scale precisely with resizing, move to GridBagLayout for column/weight control as suggested.

Recommended Answers

All 4 Replies

If you really want fine control of your layout, while retaining the platform-independence and re-sizing ability, the GridBagLayout layout manager has just about anything you can imagine. There's a bit of a learning curve, but it's well worth the effort. Tutorials and reference materials on the web as usual.

Thank you James, I will look into GridBagLayout manager :-)

Thanks ztini, appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.