Adding 100 single digit numbers to a Text Field
Ok so I have been trying to figure this out with no luck whatsoever, I have tried multiple website, irc channel to no avail.
The problem is I cant find a way to recursivly add random generated numbers to a Text Field.
The script is going to be a 100 digit random number generator, this is what I currently have so far, any help would be greatly appreciated.
Code :
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
public class RandomNumber extends JFrame implements ActionListener {
public JTextField textField;
public JButton btnGenerate = new JButton("Generate");
public String numbers = null;
Random randomGenerator;
public static void main(String[] args) {
RandomNumber app = new RandomNumber();
}
public RandomNumber() {
super("Number Generator");
setSize(400, 120);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(10, 11, 364, 20);
getContentPane().add(textField);
btnGenerate.setBounds(150, 42, 100, 23);
getContentPane().add(btnGenerate);
btnGenerate.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnGenerate) {
for (int x = 0; x < 100; x++) {
int num = randomGenerator.nextInt(10);
//this is were I'm having troubles
}
}
}
}
`~ Cody Reuille ~`
Re: Adding 100 single digit numbers to a Text Field
a) JTextField contains text, so if you want to place an int into the JTextField you need to convert the int to a String - see the static methods of the Integer class. b) If you want to append, you need to get the current text, append the new text, then set the text. The JTextField API (actually inherited from JTextComponent) outlines the methods to get and set the Text
Re: Adding 100 single digit numbers to a Text Field
I solved it
Code :
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
public class RandomNumber extends JFrame implements ActionListener {
public JTextField textField;
public JButton btnGenerate = new JButton("Generate");
int rndoutcome = 0;
public String numbers;
Random randomGenerator;
public static void main(String[] args) {
RandomNumber app = new RandomNumber();
}
public RandomNumber() {
super("Number Generator");
setSize(400, 112);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(10, 11, 364, 20);
getContentPane().add(textField);
btnGenerate.setBounds(150, 42, 100, 23);
getContentPane().add(btnGenerate);
btnGenerate.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnGenerate) {
textField.setText(null);
for (int x = 0; x <= 100; x++) {
try {
rndoutcome = (int) (Math.random() * 10);
numbers = (textField.getText());
textField.setText(numbers + rndoutcome);
} catch (NullPointerException a) {
System.out.println(a.getMessage());
}
}
}
}
}