• Post Reply Bookmark Topic Watch Topic
  • New Topic

Text Alignment

 
may tan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello..

can anyone tells me how to align my texts in text box to the center??

eg: i got 1 text field, when user input texts, it will appear in the center.. rather by defaults (at e left side)..
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
textField.setHorizontalAlignment(JTextField.CENTER);

you will find answers to most questions like this in the api docs, which
you can download and have on your pc.
 
may tan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.. thanks for your response..

i have this code in my program:

band1 = new Label("Color of Band 1", Label.CENTER);
text1 = new TextField(3);
text1.setBackground(Color.black);
text1.setHorizontalAlignment(TextField.CENTER);

pls look at my last sentence: "text1.setHorizontalAlignment(TextField.CENTER);"

why it doesn't seem to work? as in e texts are still not aligned to the cneter..
[ August 13, 2005: Message edited by: may tan ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my previous reply is for swing's JTextField

as you are using awt, you'll probably have to do it manually
i.e. calculating the string length and adding a padding - remember to
strip the padding when calling getText().
if not using a monospaced font, look up the FontMetrics class for calculation
of the string length in pixels etc
 
may tan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm.. is there really no other coding such that when user type in something in the textfield, all the texts will appear in the center instead of by default(left side)?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't use awt components enough to be sure, but I doubt it.

I'm sure this question would have been asked previously, so you're probably
better off searching for a definitive answer - try this forum, then the sun
forum, then google.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!