Setting width of JTextField

R

Rolf

Hi:
I need to create a JTextFiled that is 8 characters in width. This field
needs to only accept 8 characters. When I create a JTextField as:

customerTextField = new JTextField(8);

The displayed JTextField can actially show 12 digits at a time. Why is
the created JTextField not 8 but 12 characters wide? Is this a font problem?

Also, is there a way to "block" the entry of more than 8 digits?

TIA

- Rolf.
 
C

Chris Smith

Rolf said:
I need to create a JTextFiled that is 8 characters in width. This field
needs to only accept 8 characters. When I create a JTextField as:

customerTextField = new JTextField(8);

The displayed JTextField can actially show 12 digits at a time. Why is
the created JTextField not 8 but 12 characters wide? Is this a font problem?

Yes, it's a font "problem", to the extent that it's a problem at all.
Since most user interfaces use a variable-width font, there's really no
such thing as being just big enough to hold 8 characters. The same
field that's just big enough for eight '1' characters is probably far
too narrow to hold eight 'W' characters.

What the width parameter to JTextField does is set the control to be big
enough to hold so many of the widest character in your font. Often, but
not always, that's the capital W. You can either explicitly set the
field to a monospace font (Java guarantees that there's one available by
the name "Monospaced"), or settle for a field that's sometimes a little
wider than the data entered into it, or bang your head against the wall
with FontMetrics calculating the correct width for some different
character. I recommend against the last approach.
Also, is there a way to "block" the entry of more than 8 digits?

Yes. There were several responses to exactly this same question in
comp.lang.java.gui today. Go take a look over there for a number of
options.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
T

Tony Morris

Rolf said:
Hi:
I need to create a JTextFiled that is 8 characters in width. This field
needs to only accept 8 characters. When I create a JTextField as:

customerTextField = new JTextField(8);

The displayed JTextField can actially show 12 digits at a time. Why is
the created JTextField not 8 but 12 characters wide? Is this a font problem?

Also, is there a way to "block" the entry of more than 8 digits?

TIA

- Rolf.

You'll have to write your own Document implementation.
http://www.javaalmanac.com/egs/javax.swing.text/LimitText.html?l=find

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top