1

I have a JPanel with 3 buttons ( its a JPanel which is going to contain a menu with buttons and JLabels).

The buttons need to be stacked on top of each other, with a small space between them.

I have the buttons stacked with :

 BoxLayout myLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
 this.setLayout(myLayout);
 this.setPreferredSize(new Dimension(300,150));

Now the buttons are auto stacked when added, but they are all different size depending on the amount of text in it.

What I want is the buttons to be in the Center of the JPanel and have the width of the panel - 10 px on each side.

What i have:

give all the buttons:

    .setPreferredSize(new Dimension(280,20));

but it didnt make any diffrent.

To be more clear i will post the complete code so you can see what i mean:

private JButton buttonStartStop;
private JButton buttonReset;


public ViewControlsPanel()
{
    BoxLayout myLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
    this.setLayout(myLayout);

    this.setPreferredSize(new Dimension(300,150));

    buttonStartStop = new JButton("Start");
    buttonStartStop.setPreferredSize(new Dimension(280,30));

    buttonReset = new JButton("Reset");
    buttonReset.setPreferredSize(new Dimension(280,20));


    this.add(buttonStartStop);
    this.add(buttonReset);

}

I have tried to give diffrent dimension but it didnt help.

My guess is that it is possible to give the BoxLayout a propperty to give the containing components a preffered size but i cant find it.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169
  • 2
    See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Jan 06 '14 at 01:21

2 Answers2

2

What I want is the buttons to be in the Center of the JPanel and have the width of the panel - 10 px on each side.

This would seem best suited to a single column GridLayout with an EmptyBorder of 10 pixels (unless I misunderstand the requirement).

Single Column Of Buttons Layout

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class SingleColumnOfButtonsLayout {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new GridLayout(0,1,10,10));
                gui.setBorder(new EmptyBorder(10,10,10,10));

                gui.add(new JButton("Start"));
                gui.add(new JButton("Reset"));
                gui.add(new JButton("A Very Long String"));

                JFrame f = new JFrame("Single Column of Buttons Layout");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    This would work for any component, just change this part `gui.add(new Thing())`, where Thing is the component you want – Java Devil Jan 06 '14 at 01:57
2

This is just an extension of Andrews answer to demonstrate that you can use any component. The only changes are that I made it 2 columns wide

JPanel gui = new JPanel(new GridLayout(0,2,10,10));

and added different components

gui.add(new JLabel("Start Label", SwingConstants.TRAILING));
gui.add(new JButton("Start"));
gui.add(new JButton("Reset"));
gui.add(new JLabel("Reset Label", SwingConstants.CENTER));
gui.add(new JComboBox(new String[]{"A","B","C"}));
gui.add(new JCheckBox("A Check Box"));

Looks like this:

enter image description here

Java Devil
  • 10,629
  • 7
  • 33
  • 48