Skip to main content
added 120 characters in body
Source Link
An SO User
  • 25.1k
  • 38
  • 138
  • 230

To save yourself the hassle of a lot of if-else you can create an array of JButtons and go over them in a loop.
So button 0 will be at index 0.

Then, you can append the text to the JTextField as:

String alreadyDisplayed = in.getText(); //get the existing text String toDisplay = alreadyDisplayed + Integer.toString(loopCounter);// append the position to the text in.setText(toDisplay);// display the text 

You can loop as follows:

for(int i=0;i<jbuttonArray.length;i++){ if(e.getSource()==jbuttonArray[i]){ //insert above code here; } } 

Here is the tutorial by Oracle on this subject: http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html

To save yourself the hassle of a lot of if-else you can create an array of JButtons and go over them in a loop.
So button 0 will be at index 0.

Then, you can append the text to the JTextField as:

String alreadyDisplayed = in.getText(); //get the existing text String toDisplay = alreadyDisplayed + Integer.toString(loopCounter);// append the position to the text in.setText(toDisplay);// display the text 

You can loop as follows:

for(int i=0;i<jbuttonArray.length;i++){ if(e.getSource()==jbuttonArray[i]){ //insert above code here; } } 

To save yourself the hassle of a lot of if-else you can create an array of JButtons and go over them in a loop.
So button 0 will be at index 0.

Then, you can append the text to the JTextField as:

String alreadyDisplayed = in.getText(); //get the existing text String toDisplay = alreadyDisplayed + Integer.toString(loopCounter);// append the position to the text in.setText(toDisplay);// display the text 

You can loop as follows:

for(int i=0;i<jbuttonArray.length;i++){ if(e.getSource()==jbuttonArray[i]){ //insert above code here; } } 

Here is the tutorial by Oracle on this subject: http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html

added 183 characters in body
Source Link
An SO User
  • 25.1k
  • 38
  • 138
  • 230

To save yourself the hassle of a lot of if-else you can create an array of JButtons and go over them in a loop.
So button 0 will be at index 0.

Then, you can append the text to the JTextField as:

String alreadyDisplayed = in.getText(); //get the existing text String toDisplay = alreadyDisplayed +=+ Integer.toString(loopCounter);// append the position to the text in.setText(alreadyDisplayedtoDisplay);// display the text 

You can loop as follows:

for(int i=0;i<jbuttonArray.length;i++){ if(e.getSource()==jbuttonArray[i]){ //insert above code here; } } 

To save yourself the hassle of a lot of if-else you can create an array of JButtons and go over them in a loop.
So button 0 will be at index 0.

Then, you can append the text to the JTextField as:

String alreadyDisplayed = in.getText(); //get the existing text alreadyDisplayed += Integer.toString(loopCounter);// append the position to the text in.setText(alreadyDisplayed);// display the text 

To save yourself the hassle of a lot of if-else you can create an array of JButtons and go over them in a loop.
So button 0 will be at index 0.

Then, you can append the text to the JTextField as:

String alreadyDisplayed = in.getText(); //get the existing text String toDisplay = alreadyDisplayed + Integer.toString(loopCounter);// append the position to the text in.setText(toDisplay);// display the text 

You can loop as follows:

for(int i=0;i<jbuttonArray.length;i++){ if(e.getSource()==jbuttonArray[i]){ //insert above code here; } } 
Source Link
An SO User
  • 25.1k
  • 38
  • 138
  • 230

To save yourself the hassle of a lot of if-else you can create an array of JButtons and go over them in a loop.
So button 0 will be at index 0.

Then, you can append the text to the JTextField as:

String alreadyDisplayed = in.getText(); //get the existing text alreadyDisplayed += Integer.toString(loopCounter);// append the position to the text in.setText(alreadyDisplayed);// display the text