Brage Haavik
Brage Haavik

Reputation: 31

Displaying a random number into a Textfield using jFrame

How would I display a random number into a text field on jFrame? My method for generating a random number is in a different class.

Random rn = new Random(); 
    int i = rn.nextInt(51);
    System.out.println(i);  

How would I make the number appear in the textfield?

Upvotes: 1

Views: 2997

Answers (1)

Minudika
Minudika

Reputation: 851

You don't have to use a seperate class.

Just do as following.

Random rn = new Random(); 
JTextField textField = //initialize your text field here

Then set the random number as,

textField.setText(Integer.toString(rn.nextInt(51)));

Upvotes: 1

Related Questions