-1

i can not get my number buttons to append itself, lets say if i want to enter 11, it won't work it only show 1 in the textfield. it will not let me press another number button and append it to 1 or any number in the text field. it just stops and waits till i press a operations button

    private ActionListener NumberTapped = new ActionListener() {

        @Override
        public void actionPerformed (ActionEvent e) {
        //  JOptionPane.showMessageDialog(null, "Number tapped");

            JButton src = (JButton) e.getSource();

            String numberSelected = "";
            if (src.equals(btn0))
                numberSelected = "0";
            else if (src.equals(btn1))
                numberSelected = "1";
            else if (src.equals(btn2))
                numberSelected = "2";
            else if (src.equals(btn3))
                numberSelected = "3";
            else if (src.equals(btn4))
                numberSelected = "4";
            else if (src.equals(btn5))
                numberSelected = "5";
            else if (src.equals(btn6))
                numberSelected = "6";
            else if (src.equals(btn7))
                numberSelected = "7";
            else if (src.equals(btn8))
                numberSelected = "8";
            else if (src.equals(btn9))
                numberSelected = "9";
            else if (src.equals(btnA))
                numberSelected = "1010"; 
            else if (src.equals(btnB))
                numberSelected = "1011"; 
            else if (src.equals(btnC))
                numberSelected = "1100"; 
            else if (src.equals(btnD))
                numberSelected = "1101"; 
            else if (src.equals(btnE))
                numberSelected = "1110"; 
            else if (src.equals(btnF))
                numberSelected = "1111"; 




            if (operator.equals("") && operand1.equals("")) {
                operand1 = operand1 + numberSelected;
                outputField.setText(operand1);
            } else if (!operator.equals(""))  {//else if the operator has been set, then append the number onto operand2
                operand2 = operand2 + numberSelected;
                outputField.setText(operand2);
            }
        }    

    };
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See also this working [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Mar 05 '17 at 22:15
  • what are `operator`, `operator1` and `operator2`. Can it be the case that `operator` is empty but `operator1` is not? – Aimee Borda Mar 06 '17 at 05:34

1 Answers1

0
outputField.setText(operand1);

You can't use the setText() method. That will just replace the existing text with the new text.

Instead you need to "append" the text to the text field. An easy way to do this is to use the replaceSelection(...) method of the JTextField.

Check out: How to add strings to textPane instead of setting them? for a simple example that will allow you to simply your code by reusing a common Action and the replaceSelection(...) method.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288