Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Adding 100 single digit numbers to a Text Field

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    My Mood
    Amazed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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 ~`


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    My Mood
    Amazed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding 100 single digit numbers to a Text Field

    I solved it

    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());
    				}
    			}
    		}
    	}
    }

Similar Threads

  1. Parsing from a GUI text field
    By jdubicki in forum AWT / Java Swing
    Replies: 6
    Last Post: May 17th, 2012, 03:46 PM
  2. Text in field objects go out of bounds..
    By imsuperman05 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 5th, 2012, 06:35 PM
  3. How to generate 13 digit numbers to text file
    By duanedtp in forum Java Theory & Questions
    Replies: 3
    Last Post: April 27th, 2011, 11:30 AM
  4. Replies: 3
    Last Post: April 11th, 2011, 09:51 PM
  5. Trying to crunch a date to a single digit
    By twitch09 in forum Java Theory & Questions
    Replies: 7
    Last Post: October 15th, 2010, 12:14 AM