-1

I have a semi-complete java calculator that is not quite working properly. Whenever I hit the "1" button, it does not allow for me to add more ones. It allows me to click the button, but it only shows a single "1", rather than allowing for me to hit it multiple times and print out singular numbers. The code is done in NetBeans, and is using the design section. The link to my code is as follows: http://pastebin.com/vq9fSA2b.

I would really appreciate getting some help with this as it has created a block in my design. The error is at line 209, under btn1. I also need to add the same standard part of code under each section, but I cannot do so without having the correct code to put in.

The listener method is this:

private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                    
    String btnOneText = btn1.getText( );
    txtDisplay.setText(btnOneText);
} 
  • You are simply replacing txtDisplay with btnOneText regardless of what is in your display already? You should accumulate the results by for example reading the value and appending btnOneText – Joey Feb 06 '15 at 22:21
  • 1) See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). And I mean post it here, rather than at a link that many people will not follow. – Andrew Thompson Feb 06 '15 at 23:18

4 Answers4

1

This is what you are doing right now:

  1. Getting the text displayed on the button (presumably "1")
  2. Setting the state of the calculator to that text.

There's something missing here - you're not preserving the state of the calculator (the previously entered input) in this listener method.

This code saves the state of the input before manipulating it, appending it to the end.

private void btnActionPerformed(java.awt.event.ActionEvent evt) { 
    String inputState = txtDisplay.getText(); 
    txtDisplay.setText(inputState + ((JButton) evt.getSource()).getText()); 
}

Edit: Fixed the parentheses around the cast.

jdphenix
  • 15,022
  • 3
  • 41
  • 74
1

On line 210 you have:

txtDisplay.setText(btnOneTxt);

What that does is set the entire contents of the JTextField txtDisplay to btnOneTxt. So each time you click the button, you replace what's already in txtDisplay with btnOneTxt.

What you want to do is append to what is already in the text field with the button text, like this

txtDisplay.setText(txtDisplay.getText() + btnOneText);

Just as a side note however, your GUI code is really messy. I'm not sure how much you wrote or how much was given to you to prompt the assignment, but you might consider storing the buttons in an array so you can execute the same function on all of them with a loop.

Zach Olivare
  • 3,805
  • 3
  • 32
  • 45
  • I wasnt making the source code myself, it is just automatically done by NetBeans when editing the actual panels and such. – NebulaCoding Feb 07 '15 at 01:12
1

setText does just that, it sets the text you supply to the field, discarding what ever was previously there

There are multiple ways you might append the text instead...

You could...

Do simple String concatenation...

String btnOneText = btn1.getText();
btnOneText = txtDisplay.getText() + btnOneText;
txtDisplay.setText(btnOneText);

You could...

Use a StringBuilder and append the text each time one of the buttons is pressed...

private StringBuilder displayText = new StringBuilder(128);
//...
displayText.append(btnText);
txtDisplay.setText(displayText);

You could...

Insert the text directly into the underlying Document itself...

try {
    Document doc = txtDisplay.getDocument();
    doc.insertString(doc.getLength(), btnText, null);
} catch (BadLocationException exp) {
    exp.printStackTrace();
}

This approach doesn't require you to extract the value of the underlying Document to a String, update the String (ie concatenate it) and then reapply the String back to the Document, it injects the one String straight into the underlying Document which is generally more efficient

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Even though the post is not quite clear, but I suppose you are trying to compose number entries as new buttons are clicked.

You should be pulling the current txtDisplay content, then append the new key text to it and not erase it each time the button is clicked:

private void btn1ActionPerformed(java.awt.event.ActionEvent evt)
{                                    
    String btnOneText = btn1.getText();
    String oldTextEntry = txtDisplay.getText();
    txtDisplay.setText(oldTextEntry + btnOneText);
} 
tmarwen
  • 15,750
  • 5
  • 43
  • 62