Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Centering Text Label  RSS feed

 
Mike Parish
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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()
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • 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.
 
Maki Jav
Ranch Hand
Posts: 449
  • Mark post as helpful
  • send pies
  • 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: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!