944,050 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8937
  • Java RSS
Apr 5th, 2005
0

How do you center the text in a JTextField??

Expand Post »
I can do it with a JLabel or a JButton, but can't figure out how in a JTextField. I thought I had found it (txtLonDay.setHorizontalAlignment(JTextField.CENTER)), but that doesn't work the way I want. I want all text entered in the field to be centered, and also all text that is displayed in the field, having been produced by the program. These JTextFields are serving double duty; they accept input and also display the results of processing.

Thanks in advance,

Chuck

Don't force it... Get a bigger hammer!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ecyarter is offline Offline
10 posts
since Jan 2005
Apr 5th, 2005
0

Re: How do you center the text in a JTextField??

subclass it to provide the behaviour you want.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Apr 5th, 2005
0

Re: How do you center the text in a JTextField??

Quote originally posted by jwenting ...
subclass it to provide the behaviour you want.

Thanks... Where could I find an example of that?

Chuck
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ecyarter is offline Offline
10 posts
since Jan 2005
Apr 5th, 2005
0

Re: How do you center the text in a JTextField??

Why don't you take the number of characters the textfield is long, and how many characters the input is long, and formulate a center point out of that...Of course I would rather use setHorizontalAlignment();
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 5th, 2005
0

Re: How do you center the text in a JTextField??

Quote originally posted by server_crash ...
Why don't you take the number of characters the textfield is long, and how many characters the input is long, and formulate a center point out of that...Of course I would rather use setHorizontalAlignment();
Thanks; I had considered doing that, but I (accidentally) found my real problem: The class I was filling the textfield from had an extra /t (tab) in it. When I took that out, everything worked the way I expected.

The punctuation always throws me... :o
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ecyarter is offline Offline
10 posts
since Jan 2005
Dec 1st, 2011
0
Re: How do you center the text in a JTextField??
txtName..setHorizontalAlignment(JTextField.CENTER);



Colum
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cbrolly21 is offline Offline
2 posts
since Dec 2011
Dec 1st, 2011
1
Re: How do you center the text in a JTextField??
@cbrolly21 ... serious..
the last post here was in april 2005 ... not to mention the facts that
a. what you suggested here, has already been suggested before
b. the OP already said his problem was solved

so ... how exactly does this post contribute to the thread?
Reputation Points: 938
Solved Threads: 356
Posting Maven
stultuske is offline Offline
2,523 posts
since Jan 2007
Dec 1st, 2011
0
Re: How do you center the text in a JTextField??
It doesn't really but was the first post that came up on google
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cbrolly21 is offline Offline
2 posts
since Dec 2011
Dec 1st, 2011
1
Re: How do you center the text in a JTextField??
Try...
Java Syntax (Toggle Plain Text)
  1. txtName.setHorizontalAlignment(JLabel.CENTER);
I usually do this for JLabels, but is works on JTextFields as well.
Last edited by hfx642; Dec 1st, 2011 at 7:21 pm.
Reputation Points: 236
Solved Threads: 102
Posting Pro
hfx642 is offline Offline
509 posts
since Nov 2009
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: Creating GUI for ChatClient
Next Thread in Java Forum Timeline: Java- Payroll program help with String.valueOf()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC