• Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing - a field for integers (alternative to JTextField?)

 
David Kaplowitz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have the following code:



The look of the field on the gui is a little slit, thus is unreadable to the user. If I try to expand the size of the field with a string of white space (see commented line in the code), it throws errors when the user enters a value. I want to expand to some type of default open field for the user to enter a value between 1-70, but I don't really know how to format this field to make it look readable.

Thanks for any pointers on what I should be using either instead of JTextField or how I'm misusing it.

Dave
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try JSpinner in combination with SpinnerNumberModel and optionally JSpinner.NumberEditor instead.
 
Gerbrand van Dieijen
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your code not working correctly, or do you want the user not to allow to enter anything but numbers?

For the latter to work, you can set an inputVerifier for the text field. See javadoc for more information:
- http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JComponent.html#setInputVerifier(javax.swing.InputVerifier) and

In the verify method you can check if a number is added, and you can optionally remove the number from the JTextField value, or disallowing the textfield from loosing focus.

Also, I think it's better design to declare the JTextField at class level (so not inside the method), and initialize the the field in some init method: that helps keeping to code more structured.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Gerbrand van Dieijen You will find several other people from the Netherlands and Belgium are frequent Ranch contributors.
 
Malcolm Storey
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Kaplowitz:
Hello,

I have the following code:



The look of the field on the gui is a little slit, thus is unreadable to the user. If I try to expand the size of the field with a string of white space (see commented line in the code),


Try a different constructor

Constructs a new empty TextField with the specified number of columns.
 
Gerbrand van Dieijen
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Welcome to JavaRanch, Gerbrand van Dieijen You will find several other people from the Netherlands and Belgium are frequent Ranch contributors.


Thanks for the welcome :-)
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can create DocumentFilter, like I have.

Something about it:
Create DocumentFilter for field's document where you override its methods and check the text with specific Pattern to accept typed key or not, if decimal then one dot can be typed.
For field constructor's attribute have the class of Number what will be used (eg. Integer, Double, ...).
Number value of the field will be created like this (Number)numberClass.getConstructor(new Class[]{java.lang.String.class}).newInstance(text); ("0" for text.length() == 0).
Then implement AccessibleValue to AccessibleJTextField by extending it and fire its AccessibleContext.ACCESSIBLE_VALUE_PROPERTY by help of DocumentListener where also call java.awt.Toolkit.getDefaultToolkit().beep() if non numeric key was typed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!