We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,453 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How do you center the text in a JTextField??

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!

6
Contributors
8
Replies
6 Years
Discussion Span
1 Year Ago
Last Updated
11
Views
ecyarter
Newbie Poster
10 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

subclass it to provide the behaviour you want.

jwenting
duckman
Team Colleague
8,522 posts since Nov 2004
Reputation Points: 1,656
Solved Threads: 345
Skill Endorsements: 19

subclass it to provide the behaviour you want.

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

Chuck

ecyarter
Newbie Poster
10 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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();

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
Skill Endorsements: 22

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

ecyarter
Newbie Poster
10 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

txtName..setHorizontalAlignment(JTextField.CENTER);

:)

Colum

cbrolly21
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

@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?

stultuske
Industrious Poster
4,382 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

It doesn't really but was the first post that came up on google

cbrolly21
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Try...

txtName.setHorizontalAlignment(JLabel.CENTER);

I usually do this for JLabels, but is works on JTextFields as well.

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0875 seconds using 2.8MB