views:

266

answers:

7

Hi, 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);
+1  A: 

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).

Jeff Storey
A: 

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.

camickr
A: 

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);
akf
is it possible to have both gridBagLayout and flowLayout concurrently?
Jessy
Yes and No. :) A `Container` can only have one `LayoutManager`. However, within your GridBagLayout, you can add `JPanels`, and those `JPanels` can use their own `LayoutManager`, so you can have a `FlowLayout` within one of those panels.
akf
Thanks akf, I have solved the problem with your code :-) ...
Jessy
What do you mean his code? The code from the tutorial shows you exactly how to do this. All you need to do is read the tutorial first. And did I not state you can use a "combination of layout managers"? Do you even read what people suggest?
camickr
A: 

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);
....
Jessy
A: 

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.

willcodejavaforfood
Writing the simplest, most maintainable code should be the goal. That means using the appropriate layout manager for the job. It does not mean forcing every layout to use the same layout manager. GridBagLayout was originally used by IDE's because IDE's aren't smart enough to break the GUI down into smaller logical groupings, resulting in complicated non maintainable code where changing one constraint affects the rest. Based on your suggestion everybody should be using the GroupLayout since it is the latest specialty layout for IDEs resulting is just a much unreadable, unmaintainable code.
camickr
A: 

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);
    }
Telcontar
A: 

I am just wondering if it's possible to add array of buttons in situation where buttons are more than two.Say 10 for instance.

Babalawo