Spikatrix
Spikatrix

Reputation: 20252

How to center align everything in JPanels having GridLayout

Recently,I had asked a question about how to make JPanels align from top to bottom in a JFrame. Users told me to use GridLayout(). I got everything working,but I forgot to mention that I wanted to center align all the components in the JPanels.

My first JPanel has a JLabel.
My second JPanel has 5 JRadioButtons.
My third JPanel has a JLabel.
My fourth JPanel has a JButton.

Selecting any JRadioButton would change the text of the JLabel in the third JPanel.When the program starts,the JLabel has no text.

My JFrame has GridLayout. All 4 JPanels which have BoxLayout(BoxLayout.X_Axis) are added into this JFrame.Here is a snippet of my code:

class GUI extends JFrame implements ActionListener {

  JPanel pan1,pan2,pan3,pan4;                   //4 JPanels
  JRadioButton rad1,rad2,rad3,rad4,rad5;   //5 RadioButtons
  JButton button;                          //A JButton
  JLabel label;                            //A JLabel
   public GUI(String header)
   {
      super(header);

      setLayout(new GridLayout(0,1,0,6));  //set GridLayout to JFrame
      setBounds(350,325,600,125);
      setResizable(false);

      creator();
      adder();
      commander();

      add(pan1);
      add(pan2);
      add(pan3);
      add(pan4)  //Add all 4 panels to JFrame

  }


  private void adder()
  {
    pan1.setLayout(new BoxLayout(pan1,BoxLayout.X_AXIS));
    pan2.setLayout(new BoxLayout(pan2,BoxLayout.X_AXIS));
    pan3.setLayout(new BoxLayout(pan3,BoxLayout.X_AXIS));
    pan4.setLayout(new BoxLayout(pan4,BoxLayout.X_AXIS)); //Layout for all 4 JPanels

    pan1.add(new JLabel("Choose a Security Level"));

    ButtonGroup group=new ButtonGroup();

    group.add(rad1);
    group.add(rad2);
    group.add(rad3);
    group.add(rad4);
    group.add(rad5);

    pan2.add(rad1);
    pan2.add(rad2);
    pan2.add(rad3);
    pan2.add(rad4);
    pan2.add(rad5);

    pan3.add(label);

    pan4.add(button);
  }

   private void creator()
   {
      pan1=new JPanel();
      pan2=new JPanel();
      pan3=new JPanel();
      pan4=new JPanel();

      rad1=new JRadioButton("Security Level 1");
      rad2=new JRadioButton("Security Level 2");
      rad3=new JRadioButton("Security Level 3");
      rad4=new JRadioButton("Security Level 4");
      rad5=new JRadioButton("Security Level 5"); 

      button=new JButton("Move On");

      label=new JLabel();
   }

   private void commander()
   {
    rad1.addActionListener(this);
    rad2.addActionListener(this);
    rad3.addActionListener(this);
    rad4.addActionListener(this);
    rad5.addActionListener(this);

    rad1.setActionCommand("radio1");
    rad2.setActionCommand("radio2");
    rad3.setActionCommand("radio3");
    rad4.setActionCommand("radio4");
    rad5.setActionCommand("radio5");

    button.addActionListener(this);
   }

   public void actionPerformed(ActionEvent evt)
   {
   //When button is pressed,the text in label changes

   if(evt.getActionCommand().equals("radio1"))
      label.setText("Very Easy to bypass");
    else if(evt.getActionCommand().equals("radio2"))
      label.setText("Easy to bypass");
    else if(evt.getActionCommand().equals("radio3"))
      label.setText("Can bypass Sometimes");
    else if(evt.getActionCommand().equals("radio4"))
      label.setText("Hard to bypass");
    else if(evt.getActionCommand().equals("radio5"))
      label.setText("Very Hard to bypass");
    else
    { //Code here
    }

    repaint();
    //More code here....
   }


}

And this is the output that I get(Ignore the green lines): My program

I want everything to be aligned in the center rather than in the left. What should I do in order to align it?

Upvotes: 1

Views: 2670

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

Change:

pan1.setLayout(new BoxLayout(pan1,BoxLayout.X_AXIS));

To something like:

pan1.setLayout(new FlowLayout(FlowLayout.CENTER));

..And the same with the rest of the box layouts.

Upvotes: 3

Related Questions