• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Text Alignment

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)..
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic