Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Button sizes

Status
Not open for further replies.

jeremytaffy

Technical User
Sep 6, 2001
75
US
I have various buttons which are of different sizes. I want to make them all the same size despite the different text which is in them. If there is a way to do that, please let me know. Thanks.
 
Layout manager problems ?

Ok - here's what I would do. Create a JPanel and set the Layout Manager to null for absolute positioning. This means there will be no layout manager for the JPanel to apply its sizing rules to your buttons

Then add the buttons to the JPanel. Use setBounds() on the buttons to define absolute position and size.

Off the top of my head something like :
JPanel panel = new JPanel();
panel.setLayout(null); // no layout manager
JButton btnOK = new JButton("OK");
JButton btnApply = new JButton("Apply");
JButton btnCancel = new JButton("Cancel");

panel.add(btnOK);
panel.add(btnApply);
panel.add(btnCancel);

//Position the buttons on the panel
btnOk.setBounds(10,10,50,30); //set at 10,10 with size 50x30
btnApply.setBounds(80,10,50,30);//set at 80,10 with size 50x30
btnCancel.setBounds(130,10,50,30);//set at 130,10 with size 50x30

//Add the panel to where abouts on the JFrame you would like.
//I'll assume you have an instance if JFrame called myFrame
//and that you want the buttons in the south of a BorderLayout (the default for JFrame)
myFrame.getContentPane().add(panel, BorderLayout.SOUTH);


Luck mate - RjB.
 
one layout manager that will make all buttons the same size is the GridLayout.

Very handy if you want standard horizontal or vertical button alignment.
 
hi

the null layout is perhaps the most comfortable way of putting components in position. it can work in both awt and swing, so there's no compulsion to use swing.

if f is ur frame, b is the button

f.setLayout(null);
b.setBounds(x,y,width,height);
f.add(b);

like this add anything u want. setBounds is there for all components not only buttons. the x,y coordinates are realtive (i.e the top left of the panel or frame u r adding to is taken as 0,0

luv
Karthik.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top