• 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

Centering Text Label

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an interesting problem. Here is the code:
public void createChoiceLayout()
{
titleField = new JLabel("Library Administration");
titleField.setFont(new Font("Library Administration", Font.BOLD, 36));
getContentPane().add(titleField, BorderLayout.CENTER);
}
The problem is that the titleField is never centered! If I substitute "EAST" or "WEST", the title is moved either to the right or left side of the JLabel.
I'm using JBuilder X, and I'm wondering if this is a bug or I just can't code correctly.
Thanks for any suggestion.javascript: x()
javascript: x()
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Parish:
This is an interesting problem. Here is the code:
public void createChoiceLayout()
{
titleField = new JLabel("Library Administration");
titleField.setFont(new Font("Library Administration", Font.BOLD, 36));
getContentPane().add(titleField, BorderLayout.CENTER);
}
The problem is that the titleField is never centered! If I substitute "EAST" or "WEST", the title is moved either to the right or left side of the JLabel.
I'm using JBuilder X, and I'm wondering if this is a bug or I just can't code correctly.
Thanks for any suggestion.javascript: x()
javascript: x()


Perhaps this is what you need -


When you add lable to center, it is added at the middle and it occupies entire space if there is nothing on EAST, WEST, NORTH and SOUTH.
HTH,
- Manish
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the JLabel api:

The alignment constants are actually defined in the SwingConstants interface but JLabel implements it so the JLabel prefix works okay.
 
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
for Swing
it's titleField.setHorizontalAlignment(JLabel.CENTER);
and
for AWT
titleField.setAlignment(Label.CENTER);
Thanks
Maki Jav
[ May 08, 2004: Message edited by: Maki Jav ]
 
Mike Parish
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody.
 
reply
    Bookmark Topic Watch Topic
  • New Topic