Positioning a button on a JPanel

2.7k views Asked by At

I am trying to set the three buttons in the middle of this JPanel which is set above another panel.

Everything is working fine, but three buttons remains at the same position, no matter what.

How can I move the three buttons in the center of the panel2? Right now the three buttons are at the center Left of the panel2.

Code for my panel is here:

public AbcGeniusPanel()
 {
   //this.setVisible(false);
   ImageIcon[] alphabets = new ImageIcon[26];
   ImageIcon[] images = new ImageIcon[26];
   setBackground(Color.yellow);

   //Load the images for alphabet images into the alphabets array using a for loop 
  for(int i = 0; i < alphabets.length; i++)
    {
    alphabets[i] = new ImageIcon("C:\\Users\\Dip\\Desktop\\Java Projects\\AbcGeniusApp\\src\\Alphabets\\"+(i+1)+".png");
    }

  //Load the images images in the IMageIcon array
  for(int i = 0; i < images.length; i++)
    {
    images[i] = new ImageIcon("C:\\Users\\Dip\\Desktop\\Java Projects\\AbcGeniusApp\\src\\Images\\"+(i+1)+".png");
    }

    //Create two JPanel objects
    JPanel panel = new JPanel();
    JPanel panel2 = new JPanel();
   //Set a layoutManager on the panel

    panel.setLayout(new GridLayout(2, 13, 5, 5)); //This is good for now

    //Create an array for holdoing the buttons
    buttons = new JButton[26];

    /
    //Try passing Images inside the JButton parameter later.
    for(int i = 0; i < 26; i++)
    {
    buttons[i] = new JButton(alphabets[i]);
    }

    setLayout(new BorderLayout(2,0));
    panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS));
    //add the panel to the Border layout
    add(panel,BorderLayout.SOUTH);
    add(panel2);

    //Add evenHandling mechanism to all the buttons
     for(int k = 0; k<26; k++)
     {
      buttons[k].addActionListener(this);
     }
    for(int count1 = 0; count1<26; count1++)
    {
     panel.add(buttons[count1]);
    }

    JButton button1 = new JButton();
    JButton button2 = new JButton();
    JButton button3 = new JButton();

    panel2.add(button1);
    panel2.add(button2); 
    panel2.add(button3);
     }
2

There are 2 answers

0
black panda On

You can use the BoxLayout (it may be easier just using Box.createHorizontalBox()) but you have to put vertical glue at each end of the box. You also may want to put horizontal struts between the buttons to give them some spacing.

It will be easier to just use a FlowLayout for your buttons, which does the equivalent of what I said without the extra code. There may be a potential drawback of the layout causing a button or 2 to flow over to the next line, but with your simple application it is probably not much of a chance.

Here are the two examples. Comment out one line and Comment in (???) the other line to see a different approach to the buttons:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class AlphabetExample {

   public static void main(String[] args) {
      AlphabetExample alphabetExample = new AlphabetExample();
      JFrame frame = alphabetExample.createGui();
      frame.setVisible(true);
   }

   private JFrame createGui() {
      JFrame frame = new JFrame("Letters!");
      frame.setSize(400, 300);

      Container contentPane = frame.getContentPane();
      contentPane.add(setupLetters(), BorderLayout.CENTER);
//      contentPane.add(setupButtonsWithBox(), BorderLayout.NORTH); // <-- with a BoxLayout
      contentPane.add(setupButtonsWithFlowPane(), BorderLayout.NORTH); // <-- with a FlowLayout

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      return frame;
   }

   private JPanel setupLetters() {
      String  letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

      JPanel lettersPanel = new JPanel(new GridLayout(2, 13, 5, 5));

      for (char x : letters.toCharArray()) {
         final String letter = String.valueOf(x);
         JButton button = new JButton(letter);
         button.setActionCommand(letter);
         lettersPanel.add(button);
      }

      return lettersPanel;
   }

   private JComponent setupButtonsWithBox() {
      Box b = Box.createHorizontalBox();
      b.add(Box.createHorizontalGlue());
      b.add(new JButton("Left Button"));
      b.add(Box.createHorizontalStrut(5));
      b.add(new JButton("Center Button"));
      b.add(Box.createHorizontalStrut(5));
      b.add(new JButton("Right Button"));
      b.add(Box.createHorizontalGlue());
      return b;
   }

   private JComponent setupButtonsWithFlowPane() {
      JPanel panel = new JPanel(); // default layout manager is FlowLayout
      panel.add(new JButton("Left Button"));
      panel.add(new JButton("Center Button"));
      panel.add(new JButton("Right Button"));

      return panel;
   }
}
0
hacked009 On

This solved my problem

     for(int count1 = 0; count1<3; count1++)
    {
     panel2.add(Box.createHorizontalGlue());
     panel2.add(imageButtons[count1]);
     panel2.add(Box.createHorizontalGlue());   
    }