• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Setting size of JButton

 
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to set size of Jbutton. It's taking all the space in the frame.
 
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't used Swing since I created a document editor at university for a project over 15 years ago, but as far as I remember, you add buttons to a JPanel and not directly to the JFrame. So you're looking at something like the below, but it might not be the best way to do it.

 
Sheriff
Posts: 28347
97
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll want to learn about LayoutManagers to solve that problem. You've "decided" to use the default LayoutManager, which I forget what that is but anyway it makes components take up as much space as possible.

Here's the tutorial: A Visual Guide to Layout Managers.
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul. Looking at it
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) The recommended way is to use setPreferredSize() and not setBounds()
2) You need to use a layout for the parent which will honour the preferred size.

So effectively
Create a JPanel instance
It defaults to FlowLayout which DOES honour the preferred size
Set the preferred size to the button
Add the button to the panel
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you want to set the size of the button? Why not let it be the size it naturally prefers, based on the text it contains? Setting its exact size is rarely a good idea and might make your code non-portable since some other platform might have a different default font, or might have a different screen resolution. and it means that if you ever decide to use a different font than the default, or use a different text, you will have to re-measure everything and modify all of your sizes so the text fits. You should be using a layout manager that does all this for you.
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not getting most of the replies
But I'm able to get it to center using BorderLayout.
@Maneesh - I was not using Jpanel anywhere. I'll take a look for its functionality.

Fred Kleinschmidt wrote:You should be using a layout manager that does all this for you.


@Fred - Which layout manager does this automatically ?
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried but not getting how to use JPanel.

 
Marshal
Posts: 80138
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No layout manager does everything automatically; you must give it the details of the sizes you require. Don't make the frame and the panel fields; make them local variables. Don't use several methods to set up the GUI. This is the one occasion where you are allowed a very long method (or very long constructor). Don't set your layout to null. Don't use setSize and setBounds; use setBounds only for the top‑level container (=the frame). Don't call setVisible twice; call it once on the frame, but it shoiuld be the last statement.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shubham Semwal wrote:I tried but not getting how to use JPanel.



Note the code comments



 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Maneesh. I'm able to see panel now.
 
That is a really big piece of pie for such a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic