title: How can I display two Radio Buttons in the same row?
body: <p>I am a beginner in Java. I have been assigned the task of creating a Java window wherein which any user should be able to enter their personal data (name, last name,gender,etc) and then finally click on a submit button.</p>
  
  <p>To create such window, I have added a <code>BorderLayout</code> and four panels using <code>GridLayout</code>. The main problem with my program, which keeps me stuck, is that I am unable to put two Radio Buttons in the same row. I have tried using both <code>GridLayout</code> and <code>TableLayout</code> inside a <code>JPanel</code> to solve the problem without any success.</p>
  
  <p>This is the screenshot of how it looks:</p>
  
  <p><img src="https://i.stack.imgur.com/lxI6z.png"></p>
  
  <p>As you can see in the picture, the "F" Radio Button should be placed next to the "M" Radio Button and not below.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.GridBagLayout;
  import java.awt.GridLayout;
  import javax.swing.*;
  import javax.swing.border.EmptyBorder;
  
  public class Finestra extends JFrame
  
  {
  
  public Finestra ()
  
  {
      super("Dati Utente");
      this.setBounds(300,50,400,500);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container c = getContentPane();
      c.setLayout(new BorderLayout());
  
      Panel panel1 = new Panel (new GridLayout(1,1));
      Panel panel2 = new Panel (new GridLayout(1,1));
      Panel panel3 = new Panel (new GridLayout(5,1));
      Panel panel4 = new Panel (new GridLayout(5,1));
  
      panel1.setPreferredSize(new Dimension(100,50));
      panel2.setPreferredSize(new Dimension(100,50));
      panel3.setPreferredSize(new Dimension (100,200));
      panel4.setPreferredSize(new Dimension (260,200));
  
      c.add(panel1, BorderLayout.NORTH);
      c.add(panel2, BorderLayout.SOUTH);
      c.add(panel3, BorderLayout.WEST);
      c.add(panel4, BorderLayout.EAST);
  
      JLabel label1 = new JLabel ("Personal Data");
      label1.setHorizontalAlignment(SwingConstants.CENTER);
      label1.setFont(new Font ("Open Sans",Font.BOLD,20));
      panel1.add(label1);
  
      JLabel label2 = new JLabel ("Name");
      label2.setFont(new Font ("Open Sans", Font.PLAIN,14));
      label2.setBorder(new EmptyBorder(0,15,0,0));
  
      JLabel label3 = new JLabel ("Last Name");
      label3.setFont(new Font ("Open Sans", Font.PLAIN,14));
      label3.setBorder(new EmptyBorder(0,15,0,0));
  
      JLabel label4 = new JLabel ("Gender");
      label4.setFont(new Font ("Open Sans", Font.PLAIN,14));
      label4.setBorder(new EmptyBorder(0,15,0,0));
  
      JLabel label5 = new JLabel ("Hobby");
      label5.setFont(new Font ("Open Sans", Font.PLAIN,14));
      label5.setBorder(new EmptyBorder(0,15,0,0));
  
      JLabel label6 = new JLabel ("Nationality");
      label6.setFont(new Font ("Open Sans", Font.PLAIN,14));
      label6.setBorder(new EmptyBorder(0,15,0,0));
  
      panel3.add(label2);
      panel3.add(label3);
      panel3.add(label4);
      panel3.add(label5);
      panel3.add(label6);
  
      JTextField f1 = new JTextField();
      JTextField f2 = new JTextField();
  
      f1.setPreferredSize(new Dimension(180,30));
      f2.setPreferredSize(new Dimension(180,30));
  
      JPanel fp1 = new JPanel (), fp2 = new JPanel ();
  
      fp1.setBorder(new EmptyBorder(15,0,0,0));
      fp2.setBorder(new EmptyBorder(15,0,0,0));
  
      fp1.add(f1);
      fp2.add(f2);
  
      panel4.add(fp1);
      panel4.add(fp2);
  
  
      JRadioButton button1 = new JRadioButton("M"),button2 = new JRadioButton(("F"));
  
  
      button1.setHorizontalAlignment(SwingConstants.CENTER);  button2.setHorizontalAlignment(SwingConstants.CENTER);
  
      ButtonGroup bgroup1 = new ButtonGroup();
  
      bgroup1.add(button1); bgroup1.add(button2);
  
      panel4.add(button1); panel4.add(button2);
  
  
      setVisible(true);
  }
  </code></pre>
  
  <p>How can I display two Radio Buttons in the same row?</p>