Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java TextField()

Status
Not open for further replies.

tamkash

Programmer
Nov 7, 2007
5
CA
Hi Guys,

I am just wondering how we show values in textField. I have created a random button which generates random number and prints number. I would like to show the random number in the textField whenever the button is pressed. Thanks.

Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Random;

public class Random2class extends Applet implements ActionListener {
   Button startButton;
   TextField textField;
   int[] anArray; 
   public void init()
   {
   //Flow Layout
   setLayout(new FlowLayout());
   startButton = new Button("Random");
   anArray = new int[10];
   textField = new TextField();
   add(startButton);
   add(textField);
   //Attach action to the components
   startButton.addActionListener(this);
   }
   
   public void actionPerformed(ActionEvent evt)
   {
	  if (evt.getSource() == startButton)
	  {   
		  Random random = new Random();
          int radiant = Math.abs(random.nextInt())%11;
          for (int i =0; i<1; i++)
          {   
        	  anArray[i]= radiant;
        	  String value = String.valueOf(anArray[i]);
        	  System.out.println(value);
        	     
          }
	  }
   }
}
 
textField.setText("I have been updated");
// do not update textField so quickly and frequently
Code:
for (int i=0; i<10; i++){
textField.setText(""+i);
}
If you update textField so frequently, you may see the textField blank and you can see the final value 9.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top