Search code examples
javaswingjtextfieldtablelayout

How to move to next token in JTextField


    else if (cmd.equals("five"))
    {
        infixInput.setText("5");
    }
    else if (cmd.equals("six"))
    {
        infixInput.setText("6");
    }

So I have it set where when you press a calc button, the number is displayed on the infix textfield, but if I type 5, then 6 for example, the 6 will just replace the 5. How do I tell it to move over a space when a button is pressed


Solution

  • You could try changing infixInput.setText("6"); to:

    String text = infixInput.getText() + "6";
    infixInput.setText(text);
    

    This will append the "6" to the end of the text already displayed in the text field.