• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

swing layout button sizes

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 http://www.keang.co.uk/hex.html, 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.
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic