• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

Swing buttons

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

Some code fragments will help a lot.
Thanks
0
Aixsar
Asked:
Aixsar
1 Solution
 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now