2

I am trying to create a 2x2 grid out of 4 buttons for a membership program I am developing. The issue I'm having is that regardless of what I do, it just shows up as a 1x4 grid. Code is as follows.

    private void buildStartupPanel()
{
    startup = new JPanel();
    startup.setLayout(new GridLayout(2,2));
    addMember = new JButton ("Add a new member");
    removeMember = new JButton ("remove Member");
    reviewMember = new JButton ("Review a Member");
    reviewAll = new JButton ("Review All Members");
    startup.add(addMember);
    startup.add(removeMember);
    startup.add(reviewMember);
    startup.add(reviewAll);
    addMember.addActionListener(this);
    removeMember.addActionListener(this);
    reviewMember.addActionListener(this);
    reviewAll.addActionListener(this);
}

When I output the result, it shows the following

Add a new Member

Remove Member

Review A Member

Review all Members

Instead of

Add a new Member Remove A Member

Review A Member Review all Members

Also if anyone could help me put a space between each of the buttons that would be great!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Matt Westlake
  • 3,499
  • 7
  • 39
  • 80

2 Answers2

4

Use the 3rd & 4th int to the constructor for spacing. Otherwise, seems to work just fine here:

Startup Pane

import java.awt.GridLayout;
import javax.swing.*;

public class StartupPanel {

    private JComponent getStartupPanel()
    {
        JPanel startup = new JPanel();
        startup.setLayout(new GridLayout(2,2,50,5));
        JButton addMember = new JButton("Add a new member");
        JButton removeMember = new JButton("remove Member");
        JButton reviewMember = new JButton("Review a Member");
        JButton reviewAll = new JButton("Review All Members");
        startup.add(addMember);
        startup.add(removeMember);
        startup.add(reviewMember);
        startup.add(reviewAll);

        return startup;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                StartupPanel sp = new StartupPanel();
                JOptionPane.showMessageDialog(null, sp.getStartupPanel());
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    +1: One critical difference: `JOptionPane` invokes `pack()`; OP's code may not. – trashgod Jun 04 '12 at 02:12
  • @trashgod 1) Until we see an SSCCE, we cannot be sure what the OP's code does - it might call `pack()` immediately after adding the constructed panel. 2) I tried it quickly in a frame with set size but no pack. Same 2x2 effect. -- Having said that, I cannot for the life of me figure why the OP is seeing a single column of 4 buttons. – Andrew Thompson Jun 04 '12 at 02:17
  • You're right about an SSCCE; I was speculating that the apparent "1x4 grid" might reflect the (default) `FlowLayout` of `JPanel`. – trashgod Jun 04 '12 at 02:25
2

thanks for the responses!! Come to find out it was my 2nd panel that I was adding to the code was misspelled (woops) and throwing everything off. Guess that's the importance of posting a full SSCCE. At least I learned how to do the spacing! thanks all!

Matt Westlake
  • 3,499
  • 7
  • 39
  • 80
  • 1
    Sorry for not accepting the answer before Andrew. I'm new to the application development world and even newer to StackOverflow. I have accepted your answer. thank you for your help – Matt Westlake Jun 10 '12 at 18:00