javaswingbeanshelljformattedtextfield

JFormattedTextField not returning correct text


I am trying to make a simple dialog box in Beanshell - it should read the contents of three editable text fields and, on button press, carry out a simple task accordingly. I am totally stumped by an error that I am getting where I cannot read the text in some of the fields.

Here’s the code:

// Set up the text fields

textField_Y= new JFormattedTextField();
textField_Y.setValue(150);
textField_Y.setColumns(4);
textField_Y.setEditable(true);

textField_X= new JFormattedTextField();
textField_X.setValue(0);
textField_X.setColumns(4);
textField_X.setEditable(true);

textField_n= new JFormattedTextField();
textField_n.setValue(20);
textField_n.setColumns(4);
textField_n.setEditable(true);

button = new JButton("Generate Stage Position List");

// some Code here to arrange the items within a GUI window

// Try to read the values
button.addActionListener(new ActionListener() { 
    actionPerformed(ActionEvent eText) {
    //Get info from dialog
    yShift = textField_Y.getText();
    xShift = textField_X.getText();
    nPos = Integer.parseInt(textField_n.getText());
    print(xshift+" "+yshift+" "+nPos);
});

I run this and the dialog box displays correctly. I don’t change any values, just click the button, and it should print “150 0 20”. Instead it prints “void void 20”. I don’t have the faintest clue why one field is returning a correct number and the other two are returning void. They should all be identical! Can anyone help?


Solution

  • First, looking at this code...

    button.addActionListener(new ActionListener() { 
        actionPerformed(ActionEvent eText) {
        //Get info from dialog
        yShift = textField_Y.getText();
        xShift = textField_X.getText();
        nPos = Integer.parseInt(textField_n.getText());
        print(xshift+" "+yshift+" "+nPos);
    });
    

    yShift != yshift and xShift != xshift. Remember, Java is case sensitive.

    I would also recommend making use of getValue instead of getText