This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to center a JLabel?  RSS feed

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I'm curious on how to center a JLabel within a panel. I've added a JTextField to the center pane. Above that I added a JLabel to identify it obviously, but it's on the far left side. Is there a way to center the JLabel in the middle?

I've tried the following, including combination's of these:

 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: The methods you have tried are pertaining to the label's contents. Your requirement is to align the label itself. Two different things.
Another hint: The label is some other component's content.
Yet another hint: The way the components are laid out in a container, is defined by the container's layout.
 
Rancher
Posts: 2930
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I've added a JTextField to the center pane. Above that I added a JLabel to identify it obviously, but it's on the far left side.



To me this indicates you are using a BorderLayout. If so, then the following should work:



However, since you say it doesn't work, then you can't be using a BorderLayout. This means we don't have enough information to solve the problem. As Maneesh has pointed out position can be controlled by the layout managers and by the painting of the component itself.

Post your SSCCE to demonstrate the problem so we don't have to guess what you are really doing.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured out why it wasn't centered, I wasn't using BorderLayout, I was applying BoxLayout. Anyways. I came across another issue here, how do I make the JTextField smaller? Perhaps to one line?

 
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
From the API for BoxLayout

BoxLayout attempts to arrange components at their preferred widths (for horizontal layout) or heights (for vertical layout). For a horizontal layout, if not all the components are the same height, BoxLayout attempts to make all the components as high as the highest component. If that's not possible for a particular component, then BoxLayout aligns that component vertically, according to the component's Y alignment. By default, a component has a Y alignment of 0.5, which means that the vertical center of the component should have the same Y coordinate as the vertical centers of other components with 0.5 Y alignment.

Similarly, for a vertical layout, BoxLayout attempts to make all components in the column as wide as the widest component. If that fails, it aligns them horizontally according to their X alignments. For PAGE_AXIS layout, horizontal alignment is done based on the leading edge of the component. In other words, an X alignment value of 0.0 means the left edge of a component if the container's ComponentOrientation is left to right and it means the right edge of the component otherwise.

 
Rob Camick
Rancher
Posts: 2930
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a SSCCE anywhere...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!