0

I am trying to enable / disable textboxes on JOption pane depending on the user inputs, below is my code;

    String input1, input2, input3, input4, input5;

    input1 = "Input value 1";
    input2 = "Input value 2";
    input3 = "Input value 3";
    input4 = "Input value 4";
    input5 = "Input value 5";

    field2.setEnabled(false); // this part is not working
    field3.setEnabled(false); // this part is not working
    field4.setEnabled(false); // this part is not working
    field5.setEnabled(false); // this part is not working

    JTextField field1 = new JTextField();
    JTextField field2 = new JTextField();
    JTextField field3 = new JTextField();
    JTextField field4 = new JTextField();
    JTextField field5 = new JTextField();
    Object[] message = {
            input1, field1, 
            input2, field2, 
            input3, field3,
            input4, field4,
            input5, field5,

    };
    int option = JOptionPane.showConfirmDialog(null, message, "Enter all your values", JOptionPane.OK_CANCEL_OPTION);
    if (option == JOptionPane.OK_OPTION)
    {
      String value1 = field1.getText();
      String value2 = field2.getText();
      String value3 = field3.getText();
      String value4 = field4.getText();
      String value5 = field5.getText();


    }

    if (value1 > 0 || value1< 8) { // this part is not working
        input1 = "Wrong input for value 1";

    }else{
        field2.setEnabled(false);
        field3.setEnabled(false);
        field4.setEnabled(false);
        field5.setEnabled(false);

    }

Basically, what I want is all the boxes to be disabled at start expect textbox 1 and if the user types in a value between 0 and 8 then it will enable the next box, else it should provide them with a error "Wrong input for value 1". I will need to do this for all the textboxes and upon the use providing the correct answer it should enable the next box.

I hope I have explained my problem correctly, if not please let me know.

jalopaba
  • 8,039
  • 2
  • 44
  • 57
  • When you say `not working`, is that block of code not disabling the text boxes at all? – christopher Jan 27 '14 at 17:48
  • 1
    You're setting properties to the fieldN before you create them in that code. Do you create them elsewhere? Is it supposed? – Hugo Sousa Jan 27 '14 at 17:52
  • @Christophere, sorry for the late reply by not working I mean, that piece of lines are giving me error. –  Jan 27 '14 at 18:55
  • @Hugo Sousa, sorry for the late reply, I am not creating the boxes elsewhere, there are only made in this piece of code. –  Jan 27 '14 at 18:57

3 Answers3

0

One problem is that you are re-creating the objects field1, field2, field3... after you have set enabled to false. Do not re-create the objects after you have disabled them. It looks like the initial creation of these objects (field1...field5) is not shown in your example.

Jeff R
  • 94
  • 6
0

It's better to use change listeners and on change event handlers, check values and enable/disbale jTextFields.

Here is an example: sample Code

 JTextField myTextField = new JTextField();
        myTextField.getDocument().addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent e) {
                // Do something
            }
            public void removeUpdate(DocumentEvent e) {
                // Do something
            }
            public void insertUpdate(DocumentEvent e) {
                // Do something
            }
        });
Mohammad Mazraeh
  • 1,044
  • 7
  • 12
0
String input1, input2, input3, input4, input5;

input1 = "Input value 1";
input2 = "Input value 2";
input3 = "Input value 3";
input4 = "Input value 4";
input5 = "Input value 5";

JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
JTextField field3 = new JTextField();
JTextField field4 = new JTextField();
JTextField field5 = new JTextField();

//move this after creating the fields
field2.setEnabled(false);
field3.setEnabled(false); 
field4.setEnabled(false); 
field5.setEnabled(false);

Object[] message = {
        input1, field1, 
        input2, field2, 
        input3, field3,
        input4, field4,
        input5, field5,

};
int option = JOptionPane.showConfirmDialog(null, message, "Enter all your values", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION)
{
  String value1 = field1.getText();
  String value2 = field2.getText();
  String value3 = field3.getText();
  String value4 = field4.getText();
  String value5 = field5.getText();


  field1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent e) {

           if (Integer.parseInt(value1) > 0 || Integer.parseInt(value1) < 8) { 
                input1 = "Wrong input for value 1";

           }else{
                field2.setEnabled(false);
                field3.setEnabled(false);
                field4.setEnabled(false);
                field5.setEnabled(false);
           }
       }
  } 
}
Hugo Sousa
  • 1,904
  • 2
  • 15
  • 28
  • if (value1 > String.valueOf(0) || value1< String.valueOf(8)) { input1 = "Wrong input for value 1"; the value1 gives me a error saying create a local variable value1. –  Jan 27 '14 at 19:03
  • You're creating the variable _value1_ inside an 'if'. The compiler doesn't know if that 'if' will succeed, so he doesn't recognize _value1_. You need to declare the variable outside the 'if'. – Hugo Sousa Jan 27 '14 at 19:44
  • Hi, I created it outside my if statement String value1,value2,value3,value4,value5; but it says I already have a variable with this name (String value1 = field1.getText(); ... String value5 = field5.getText();) –  Jan 27 '14 at 20:10
  • If you declare it outside, then you just do: value1 = field1.getText(); – Hugo Sousa Jan 27 '14 at 20:11
  • Hi, it is giving me error on this line if (value1 > String.valueOf(0) || value1 < String.valueOf(8)), saying that the < and > operator is undefined for the value string. –  Jan 27 '14 at 20:31
  • Hi, thanks this is what I wanted but however, I didn't think of this before, you know when you run this code, all but first box are disabled and when you type in a number in the first box, you have to press ok to the actions to contiune but however, pressing enter will close the JPanel. –  Jan 27 '14 at 21:03
  • What I want it to happen is, when the user types in a number, I want it to without the user pressing the OK button, to do the tasks that it is assigned to e.g. invalid input error, enabling then next box ect. –  Jan 27 '14 at 21:05
  • Sorry about this, I didn't think of this before, really sorry. –  Jan 27 '14 at 21:05
  • That way, i think you should use Mohammad Mazraeh suggestion and add a change event. When you change that jtextfield, that event will be fired and you execute the next action. Here's an example: http://stackoverflow.com/questions/3953208/value-change-listener-to-jtextfield – Hugo Sousa Jan 27 '14 at 21:40
  • Hi, Thanks hugo, I just looked at the link you gave but it looks really complex, could you please give me a example with my code, if possible? –  Jan 27 '14 at 21:55
  • Edited my answer again. I'm not testing it, so there may be some errors, I don't know. Hope it helps. – Hugo Sousa Jan 27 '14 at 21:59
  • Hi, sorry to bother you with this problem. the code it working but I had to add final to some of the variables but it still does to unlock the next box or print error upon the user's action. –  Jan 27 '14 at 22:15