Question

how can I make a button place side by side. I used a gridBagLayout to design the layout.The problem is that the button place too far from each other. I have tried to choose the CENTER as anchor but this makes the button overlapping. If I used WEST and EAST, the button placed too far from each other.

e.g. SAVE ---------- NEW PATTERN instead of SAVE NEW_PATTERN.

JButton bSave = new JButton("SAVE");
JButton bPattern = new JButton("NEW_PATTERN");
con = new GridBagConstraints();
con.anchor=GridBagConstraints.WEST;
con.gridy = 3; con.gridx = 0;           
con.gridwidth = 1; con.gridheight = 1;      
con.insets= new Insets(2,5,2,2);        
m.setConstraints(bSave, con);
c.add(bSave);
con.weightx=1;
con.gridy=3; con.gridx=0;
con.anchor=GridBagConstraints.EAST;
m.setConstraints(bPattern,con);
c.add(bPattern);
Was it helpful?

Solution

Thanks akf, I have solved the problem by placing the flowLayout inside the gridBagLayout.

....
JButton bSave = new JButton("Save");
JButton bPattern = new JButton("New Pattern");
JPanel pContainer = new JPanel();
pContainer.setLayout(new FlowLayout(FlowLayout.CENTER));
pContainer.add(bSave); pContainer.add(bPattern); 
con = new GridBagConstraints();
con.anchor=GridBagConstraints.CENTER;
con.gridy = 3; con.gridx = 0;           
con.gridwidth = 1; con.gridheight = 1;      
m.setConstraints(pContainer, con);
c.add(pContainer);
....

OTHER TIPS

GridBagLayout is the most complicated of layouts. If you're just aligning a couple of buttons, I would recommend using FlowLayout (the default) or BoxLayout. But, if you want to use GridBagLayout, instead of adjusting the anchor, adjust the gridx to be 1 for the second button. Also, not sure why you have a gridy of 3 instead of gridy of 0 (unless there is other code you have omitted that uses gridy of 0-2).

Why don't you read your old postings first before posting new questions?

You where given a link to the Swing tutorial in your last posting. So read the tutorial, try out the examples and use the appropriate layout manager or combination of layout managers for the job.

Instead of GridBagLayout,GridLayout is a simple solution. It is easy to set up:

JButton bSave = new JButton("SAVE");
JButton bPattern = new JButton("NEW_PATTERN");
JPanel panel = new JPanel(new GridLayout(1,2); // 1 row, 2 cols
panel.add(bSave);
panel.add(bPattern);

EDIT:

Just out of curiosity, I was fooling around with your original and found a combination that allows for the use of only the GridBagLayout. It is not much different than your original:

GridBagConstraints con = new GridBagConstraints();
con.anchor=GridBagConstraints.WEST;
con.gridy = 3;
con.gridx = 0;                   
con.gridwidth = 1; 
con.gridheight = 1;          
con.insets= new Insets(2,5,2,2);        
m.setConstraints(bSave, con);
c.add(bSave);
con.weightx=0;
con.gridy=3;
con.gridx=1;//this is the big diff!
con.anchor=GridBagConstraints.EAST;
m.setConstraints(bPattern,con);
c.add(bPattern);

This code situate the buttons side by side in the center of the screen.

The Key are :

constraints.fill=GridBagConstraints.NONE;--> make the butons not to expand

constraints.insets.right=0;--> makes the buttons stand side by side

constraints.insets.left=0;--> makes the buttons stand side by side

constraints.weightx=1 --> makes the cell in wich the buttons are expando horizontally

constraints.anchor=GridBagConstraints.EAST;--> makes the left button to stand at the of the cell

constraints.anchor=GridBagConstraints.WEST;--> makes the right button to stand at the left of the cell

public static void main(String args[]){
        JFrame frame=new JFrame();
        Container cont=frame.getContentPane();
        cont.setLayout(new GridBagLayout());

        GridBagConstraints constraints=new GridBagConstraints();
        constraints.insets.top=2;
        constraints.insets.bottom=2;
        constraints.insets.left=0;// increment to separate buttons
        constraints.insets.right=0;// increment to separate buttons
        constraints.fill=GridBagConstraints.NONE;
        constraints.weightx=1;
        constraints.gridy=0;

        constraints.anchor=GridBagConstraints.EAST;
        constraints.gridx=0;
        cont.add(new JButton("Save"),constraints);

        constraints.anchor=GridBagConstraints.WEST;
        constraints.gridx=1;
        cont.add(new JButton("New Pattern"),constraints);

        frame.pack();
        frame.setVisible(true);
    }

I suggest you read the tutorial for GridBagLayout since it is out of the box the most advanced layout manager and makes the other ones obsolete. It is well worth learning for those reasons. It makes everything simple if you think of it as a grid and all you have to get right then is the x and y coordinates which is what you missed in your code. You were almost there :)

Mixing Layout managers is usually a very bad idea since they all work slightly different when it comes to filling out containers with extra space.

@camickr Based on my comment people should use GridBagLayout since it is the most flexible one and can be used in any situation. Having only one should result in simple and maintainable could. I also find it very easy to read since it is logically mapped. The contstraints only change if you use one constraints object for all components, which is obviously a bad idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top