Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is is the difference between these two codes?

This code right here is a button of a calculator that is when pressed shows "2" in the calculator’s textbox
I was just curious why the first code allows me to enter number "2" just once

while the second one allows me to enter the number "2" Many times, in other words, what does textbox.getText() adds to this code?

private void BUT2ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    String enternumber =  BUT2.getText();
    textbox.setText(enternumber);
} 
private void BUT2ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    String enternumber = textbox.getText() +BUT2.getText();
    textbox.setText(enternumber);
} 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The first snippet copies the label of the button ("2") into the text field, replacing its prior content.

The second snippet looks at what’s already in the text field, which could be anything, and adds the label of the button ("2") to the end of it. This way, you can add "2" to the text field as many times as you press the button.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading