This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

make JButton the same size as JLabel  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, so there is something I wanna know in how to make a better display on the screen. As you can see in the picture, I want to make the jlabel "synchronize" with the Jbutton. I have been trying to do this by enlarge the font size and creatng an empty border until the display seem good. However, it is a bit tiring and I want to know if there is any easier alternative.
abc.png
[Thumbnail for abc.png]
 
Rancher
Posts: 507
15
Java Notepad
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it can be made. But, post the code you have so far, for that display.
 
Rancher
Posts: 3040
22
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a different layout manager. For example you could use a GridBagLayout to place each component in a specific cell.

Read the section from the Swing tutorial on How to Use GridBagLayout for more information and working examples to get you started.
 
Marshal
Posts: 64471
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe grid layout is simpler, and it has the property of making all the components the same size.
 
Saloon Keeper
Posts: 3250
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually you don't want the buttons to be as large as the labels, changes are the buttons will be way too big. Robs suggestion is simple, but the Java tutorial gives another way to get the required layout, via the SpringLayout (needs the SpringUtilities class; never used it myself, but it looks simple as well):

SpringLayout

And although much frowned upon here, but using a GUI builder is also simple.
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried spring layout a long time ago and couldn't seem to get it to work, so I haven't tried again.
The problem with a GUI builder is that it produces screeds of group layout code which is incomprehensible to inexperienced users, so they can't correct any problems with the layouts. GUI bulders are probably good for more experienced people.
 
Piet Souris
Saloon Keeper
Posts: 3250
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed it is incomprehensible code, but that is not the IDE's fault, it is just how Group layout works.  You have the option to hide the code in the IDE and never look at it. Just as if you are using a HashMap, you are not interested in its code, you only use it. That is not to say that a GUI builder is without problems. Sometimes you drag a component, only to find out your complete layout becomes a mess, suddenly, making you decide to do it manually after all.
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . You have the option to hide the code in the IDE and never look at it. . . . .

If you are happy with that, then a GUI builder is all right. But people learning Java® have to be able to create their own layoiuts.

That is not to say that a GUI builder is without problems. Sometimes you drag a component, only to find out your complete layout becomes a mess . . . .

In which case you need the classic tutorial
 
Lando Chan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. It works. This is a small piece of my code so others could learn as well

cba.png
[Thumbnail for cba.png]
 
Piet Souris
Saloon Keeper
Posts: 3250
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative could be to have a, say, JList of names, and just one button, that looks for the selected item and plays it.
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that solution scalable? The scroll pane is working for four rows of components, but what would happen with seven rows?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!