Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting size of JButton

 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to set size of Jbutton. It's taking all the space in the frame.
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • 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.

 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Paul. Looking at it
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Fred Kleinschmidt
Bartender
Posts: 470
3
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
I tried but not getting how to use JPanel.

 
Campbell Ritchie
Marshal
Posts: 52630
119
  • Mark post as helpful
  • send pies
  • 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: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Maneesh. I'm able to see panel now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!