Reputation: 3651
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!
Upvotes: 2
Views: 1472
Reputation: 3651
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!
Upvotes: 2
Reputation: 168825
Use the 3rd & 4th int
to the constructor for spacing. Otherwise, seems to work just fine here:
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());
}
});
}
}
Upvotes: 4