Swing buttons

How do prevent Swing buttons from changing sizes when the window is resized?

Some code fragments will help a lot.
Thanks
AixsarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kylarCommented:
It depends on the layout manager you're using. Some layout managers stretch things when they are resizing, and others don't. Can you give us a little more information about what you are trying to do?
0
expertmbCommented:
hi,
 as kylar mentioned it depends on the which layout u r using . try with null layout .

mb...
0
filipecorreiaCommented:
Yes it does depend on the layout you're using. I would advise you to use flowlayout(code A) but for something more complex you may have to use GridBagLayout(code B).


Code A:

JPanel jp = new JPanel(new Flowlayout());
jp.add(new JButton("OK"));
jp.add(new JButton("Cancel"));



Code B(got it from a tutorial):
private JPanel contentPane;
        ...
        protected void makebutton(String name,
                                  GridBagLayout gridbag,
                                  GridBagConstraints c) {
            JButton button = new JButton(name);
            gridbag.setConstraints(button, c);
            contentPane.add(button);
        }

        public GridBagWindow() {
            GridBagLayout gridbag = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();
         
            contentPane = new JPanel();
            contentPane.setFont(new Font("Helvetica", Font.PLAIN, 14));
            contentPane.setLayout(gridbag);
           
            c.fill = GridBagConstraints.BOTH;
            c.weightx = 1.0;
            makebutton("Button1", gridbag, c);
            makebutton("Button2", gridbag, c);
            makebutton("Button3", gridbag, c);

            c.gridwidth = GridBagConstraints.REMAINDER; //end of row
            makebutton("Button4", gridbag, c);

            c.weightx = 0.0;                   //reset to the default
            makebutton("Button5", gridbag, c); //another row

            c.gridwidth = GridBagConstraints.RELATIVE; //next to last in row
            makebutton("Button6", gridbag, c);

            c.gridwidth = GridBagConstraints.REMAINDER; //end of row
            makebutton("Button7", gridbag, c);

            c.gridwidth = 1;                      //reset to the default
            c.gridheight = 2;
            c.weighty = 1.0;
            makebutton("Button8", gridbag, c);

            c.weighty = 0.0;                   //reset to the default
            c.gridwidth = GridBagConstraints.REMAINDER; //end of row
            c.gridheight = 1;                   //reset to the default
            makebutton("Button9", gridbag, c);
            makebutton("Button10", gridbag, c);

            setContentPane(contentPane);
        }



Filipe
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.