Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Centering Text in a TextArea

Posted on 2002-07-03
14
804 Views
Last Modified: 2010-05-18
Hi 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--

0
Comment
Question by:dineth
  • 3
  • 3
  • 2
  • +5
14 Comments
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 7126397
Don't think that can be achieved.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7126529
you have to implement your own widget (or use some ready-made)
0
 

Expert Comment

by:sunilvirus
ID: 7126683
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
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:yasser_helmy
ID: 7126845
I dont think that can be done. I reviewed the textarea api to do this, but there were no clues. Maybe you can make some method that gives this appearance to the textarea.
I will try to write some code and send you the results.
0
 
LVL 2

Expert Comment

by:allie
ID: 7127028
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(JTextField.CENTER_ALIGNMENT), or you can insert text into the JTextArea with HTML.  If you use HTML, it'd look like this -

myTextArea.setText("<HTML><CENTER>Here is my text.</CENTER></HTML>");

I'll see if I can find anything else though.  Good luck!

allie
0
 
LVL 2

Expert Comment

by:allie
ID: 7127088
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/javanotes/30GUI/60fonts/18font.html

(By the way, the parent site is EXTREMEMLY useful.  URL is http://leepoint.net/notes/javanotes/)

Good luck!

allie


0
 

Author Comment

by:dineth
ID: 7127359
Thanks guys !! thanks a lot !!
thanks for the link Allie.

--Din--
0
 
LVL 2

Expert Comment

by:allie
ID: 7127367
You're welcome!  I take it the code at the link worked then?

allie
0
 
LVL 92

Expert Comment

by:objects
ID: 7128716
Not sure exactly how FontMetrics can help you.
Isn't the actual painting of the text handled by the peer?
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 7129317
Hi,

If you want to use TextArea, you have 2 options:
- Put TextArea into Panel and let panel center TextArea (by default, TextArea.getPreferredSize() returns text width, and Panel uses FlowLayout which will center single component)
- Use fixed width font in the TextArea and put spaces around your text to center it.

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
0
 
LVL 92

Expert Comment

by:objects
ID: 7129342
> Put TextArea into Panel and let panel center TextArea

Will that centre align the text?
I would have thought it would just align the component in the middle of the panel.
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 7129359
> Will that centre align the text?
> I would have thought it would just align the component in the middle of the panel.

Yes, but text is shown inside that centered component, so visual effect would be left-justified text centered in applet (with TextArea border around)
0
 
LVL 35

Expert Comment

by:girionis
ID: 8893873
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to objects

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 62
Desingning Refactoring existing code 2 26
maven disable workspace resolution 1 22
running on tomcat not jboss eap 7.0 3 18
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question