0

Not sure how to write this in JAVA... I need to code that will insert a default value (say 50) if user keys in a value outside of a given range of 10-100 feet. I have it working for errors if blank or non integer is entered or value is outside the range but cannot figure out how to integrate a default. What I have that works is

JPanel panel1 = new JPanel();
    panel1.setLayout(new FlowLayout());
    poolLength = new JTextField(10);
    panel1.add(new JLabel("Enter the pool's length (ft):"));
    panel1.add(poolLength);

What I want to add is something like this

If poolLength <10 or >200 then poolLength = 100
        Else this.add(panel1);
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
Bklyn_40
  • 21
  • 1
  • 1
  • 7

3 Answers3

0

disclaimer: I do not know Swing well, actually, I dont remember much at all..

Maybe you simply need to attach a validator to your JTextField?

Here you have an example, however please notice that in that post they check for the length of the text, not the contents. But having the text, you can easily parse it as a number and check if it is greater/lower than the bounds.

As you see in that post, by returning true/false you will prevent the user from entering a wrong value - ie. too small. Now, when wrong value is detected, then, instead of returning 'false', maybe it is possible to simply myjtextfield.setText("50.0") on that input? That may do exactly what you wanted.

really, be careful with those "maybe"s: I do not know Swing, but from general UI framework design, trying to "setText" may:

  • throw an exception: many frameworks think it's quite evil to change the field's value during validation and defend themselves against it
  • cause strange issues when editing: if that JTextEdit calls validation upon every single change of the text, you will notice odd things when trying to, i.e. select-all, delete, write 123. You'll end up with 50.0123 or 12350.0 as the JTextEdit might try to validate the intermediate empty text after deletion and coerce it to "50.0"..
  • or, it may just work.

So, if your time is critical, just try it. Maybe I guessed well. But, if you have some time to spare, wait until someone checks that I didn't write any nonsense.

Community
  • 1
  • 1
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
0

The simplest way is to get the text during e.g. an ActionEvent, parse it and reset the text if it's outside the range.

jTextField.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        boolean isValid = true;

        try {
            int intVal = Integer.parseInt(jTextField.getText());
            if (intVal < 10 || intVal > 200) {
                isValid = false;
            }
        } catch (NumberFormatException x) {
            // user entered something which is not an int
            isValid = false;
        }

        if (!isValid) {
            jTextField.setText("100");
        }
    }
});

Also see How to Use Text Fields.

Another way would be to use a spinner which does something like this by default.

Radiodef
  • 37,180
  • 14
  • 90
  • 125
0

Use a JSpinner instead.

enter image description here

It will not behave exactly as described, but provide a better user experience. The user can adjust the value with the / buttons. If they type a value outside the specified range, it will revert to the last valid value when the field loses focus.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433