you have to implement your own widget (or use some ready-made)
Main Topics
Browse All TopicsHi Experts !!
I got an Applet with a BorderLayout which as a TextArea added to CENTER region. I wanna center text which is placed in the TextArea.
Could this be done ?? I tried to FontMetrics & used stringWidth() method to obtain the width of the string & performed a simple calculation to get the center of the TextArea but, it failed.
I'm using an AWT TextArea.
ThankX.
--Din--
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I don't know if u can do it in a text filed.
But u can surely do it in an applet.
There is a good book on JAVA applets for startups.
The title is "JAVA Complete Reference".
I have faced this problem before with an applet.
There is a good description of how to center text in an applet.
Try to get access to that book.
ps:i think you can do it in JTextArea
Well... I don't know of any attributes you can set in a JTextArea to center text. But you've got a couple of options. You can either try to do something with a JTextField and call setHorizontalAlignment(JTe
myTextArea.setText("<HTML>
I'll see if I can find anything else though. Good luck!
allie
OK, the only other thing I found also used FontMetrics, but didn't have anything about stringWidth(), so maybe this is a better way. Check out the following web site - it explains how to use FontMetrics to center text:
http://leepoint.net/notes/
(By the way, the parent site is EXTREMEMLY useful. URL is http://leepoint.net/notes/
Good luck!
allie
Hi,
If you want to use TextArea, you have 2 options:
- Put TextArea into Panel and let panel center TextArea (by default, TextArea.getPreferredSize(
- Use fixed width font in the TextArea and put spaces around your text to center it.
Regards,
Igor Bazarny,
Brainbench MVP for Java 1
Business Accounts
Answer for Membership
by: objectsPosted on 2002-07-03 at 01:40:03ID: 7126397
Don't think that can be achieved.