• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to limit the numbers that can be entered in JFormattedTextField.  RSS feed

 
Pooja Patole
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,
I am new to Swing. I have a JTextField in which i can enter only values less that hundred and no values more than 100 should be allowed to enter in the field. what should be done...

Thanks
 
Swastik Dey
Rancher
Posts: 1801
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Retrieve the value from text field. Change it to int. Now apply a check.
 
Pooja Patole
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swastik,
I have done that already. But i want it the other way. Suppose while am entering the value in the text field and if that value is more than 100 it should notify me then and there that the value is more than 100 before i press the submit button

 
Swastik Dey
Rancher
Posts: 1801
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pooja,

When do you wish to apply the check? When the submit is pressed or when the key is pressed? Whatever is the case I guess way should be same as I suggested, correct me if I am wrong.
 
Pooja Patole
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private JPanel createActualRangePanel(){
JPanel panel = new JPanel();
panel.add(new JLabel("Actual Range : "));
rangeField = new JFormattedTextField(NumberFormat.getIntegerInstance());
rangeField.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);

try {
rangeField.setText("100");
rangeField.commitEdit();
}
catch(ParseException e) {
//Shouldn't get here unless the setText value doesn't agree
//with the format set above.
e.printStackTrace();
}
rangeField.setColumns(5);
panel.add(rangeField);
JButton setButton = new JButton("Set");
rangeField.addActionListener(this);
setButton.addActionListener(this);
panel.add(setButton);
return panel;
}


public void actionPerformed(ActionEvent e) {

int val = Integer.parseInt(rangeField.getText());
if ( val <= 100){
try {
setThumbSize(val);
_scroll.setModel(new DefaultBoundedRangeModel(selectableRange.getMin(), thumbSize, selectableRange.getMin(), selectableRange.getMax()));
_scroll.setBlockIncrement(thumbSize /10 );
_scroll.setUnitIncrement(thumbSize /100);
} catch (ThumbSizeExceededException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else
{

try {
throw new RangeOutOfBoundsExecption(100);
} catch (RangeOutOfBoundsExecption e1) {
System.err.println("RAnge must be between 0 to 100");
}
}
}

This is what i wrote initially but my Sir said that this will be checked when the button in pressed.But he wants that when I am entering the numbers and if the number goes beyond 100 the application should not accept it.So can you help me?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check out the Swing FAQ on this forum' s
main page - there's an example of textfield
limiting that you should be able to adapt
to your needs.
 
Swastik Dey
Rancher
Posts: 1801
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pooja,

Could you please paste you code inside code tags.
 
Pooja Patole
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Pooja Patole
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Michael,
I checked it out but i want something else. But thanks for your help
 
Swastik Dey
Rancher
Posts: 1801
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pooja,

You need it handle on keypress. Could you please try this code.



 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to write your own Document to do that. You can look at the example from the ranch faq. You have to use super.getLength() + str.getLength() to get the total length - key thing is that, this will cover ctrl-c,ctrl-v scenarios too.

PS: The subject says JFormattedTextField and the body of the post says JTextField. I think we need to take a different approach to JFormattedTextField even though it uses Document too.

Edit: Oops, I left the window open and replied after some time...guess I was late...ignore if already answered.
 
Pooja Patole
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Swastik,
Thanks a lot. It worked
 
Swastik Dey
Rancher
Posts: 1801
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.
 
Pooja Patole
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranganathan Kaliyur Mannar wrote:You have to write your own Document to do that. You can look at the example from the ranch faq. You have to use super.getLength() + str.getLength() to get the total length - key thing is that, this will cover ctrl-c,ctrl-v scenarios too.

PS: The subject says JFormattedTextField and the body of the post says JTextField. I think we need to take a different approach to JFormattedTextField even though it uses Document too.

Edit: Oops, I left the window open and replied after some time...guess I was late...ignore if already answered.


Hey thanks for your reply.I will surely check it out too
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!