File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes swing layout button sizes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "swing layout button sizes" Watch "swing layout button sizes" New topic

swing layout button sizes

Liam McTavish

Joined: Aug 23, 2012
Posts: 2
im making a GUI for an assignment and want to customize my button sizes. The layout uses the gridlayout layout manager. Currently i have 6 buttons each in a different row. i just want to make the buttons not take up the full width of the Jframe, rather say, 50% of the width but still centered? How do i get this layout? Heres my code:
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

I am no swing expert, but I have done this sort of thing in the past. What I ended up doing was putting a JPanel in each cell, then putting the button in the JPanel. That way the JPanel's size is controlled by the GridLayout, but the button's size is controlled by the JPanel's, and you can set the JPanel's to whatever you want - like FlowLayout, or whatever one lets you center the button, and lets the button control its own size.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
Welcome to the Ranch

as an alternative, find a copy of Core Java II by Horstmann and Cornell (vol 1, probably) and look up the GBC class and its use with GridBagLayout. You can find more details of the book in our book reviews section.
venkata vinay bysani

Joined: Feb 09, 2010
Posts: 25
If you want to change and position any component in a frame as you wish then you should not use grid lay out. Grid layout will divide the frame and it will make the component's size in a particular grid to the size of the grid.
For keeping the size and position of your components as you wish you need to do the following:
1) set the frame lay out to null by f.setLayout(null);
2) before adding a component to frame you need to mention their co-ordinates by using setBounds method. ex: addItem.setBounds(xAxis point,yAxis point,width of the componen,height of the component)
3) once you set the position and size of your components then add it to the frame

Working with setBounds initially requires some trails as you may not get the exact position in the first go you should keep on changing the values until you get to your desired position and size. Once you get some idea how it works then its easy to set the position of your components
Tony Docherty

Joined: Aug 07, 2007
Posts: 2838
For keeping the size and position of your components as you wish you need to do the following:
1) set the frame lay out to null by f.setLayout(null);

No, please don't do this, layout managers are very useful in most situations.
Not using a feature (ie a layout manager) just because you don't know how to use the feature properly does not necessarily make for a good solution.

To be honest I'm not 100% sure what layout the OP is trying to achieve as the description is not totally clear, but there is almost certainly a way to do it with either one or a combination of layout managers. I've not seen a static layout yet which I haven't been able to do with layout managers - admittedly, on a couple of occasions I've had to write my own custom layout manager but they were for very unusual layouts (hexagonal grids, etc).

@Liam McTavish: Can you provide a more detailed description and/or sketch of what you are trying to achieve and what resize behaviour etc you expect.
I agree. Here's the link:
subject: swing layout button sizes
It's not a secret anymore!