Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

center vertically jbutton inside jpanel -java

I have a border layout with 4 panels, north, south, east and west. For example in east side I have a jpanel which have four buttons. My problem is that all buttons are align to the top and I want align to the center. In css for example something like margin top or top:50%.

Any ideas?

    JPanel contentor3 = new JPanel();
  contentor.add(contentor3, BorderLayout.EAST);
  contentor3.setBackground(Color.green);
  contentor3.setPreferredSize(new Dimension(120, 750));

  contentor3.add(btn[1]);
  btn[1].setText("btn1");
like image 990
anvd Avatar asked May 18 '26 16:05

anvd


1 Answers

You need to change the layout of contentor3. Try something like:

contentor3.setLayout (new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints ();

// next two lines will place the components top-to-bottom, rather than left-to-right
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;

// add as many buttons as you want in a column
contentor3.add (new JButton ("btn1"), gbc);

contentor.add (contentor3, BorderLayout.EAST);
like image 125
Stewart Murrie Avatar answered May 21 '26 21:05

Stewart Murrie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!