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 18 of 18

Thread: Need help with some coding

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with some coding

    I'm working on a program that has three buttons... one that plays a video, one that displays information, and one that I want to take information from a random generator and display it in a text field. My first question is how do I tell the generator to display the text in the text field. My second question is, how do I get the button to run this other class.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need help with some coding

    One thing at a time. Which part of this is giving you trouble? Do you know how to process an event from a button click? Do you know how to use a random generator? Do you know how to set the text of a text field? Provide an SSCCE demonstrating exactly which step you're confused about, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with some coding

    I'm sorry... i do realize it was a bit rushed. I have no idea how to make an SSCCE but I will explain exactly what I am trying to do. I'm making a compliment generator. Using a the switch function I've set it to generate a number and then output a result. I want It to display the result next to the to "generate" button in a text field I placed next to it.

    Here's the compliment generator code so far:

    import java.util.Random;
     
    public class Compliment {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Random myRandom = new Random();
    		int randomNumber;
     
    		randomNumber = myRandom .nextInt(10) + 1;
     
    		switch (randomNumber) {
    		case 1:
    		case 2:
    		case 3:
    		case 4:
    		case 5:
    		case 6:
    		case 7:
    		case 8:
    		case 9:
    		case 10:
    		default:
     
    		}
     
    	}
     
    }

    I have the button set up with an action listener. What is the syntax required for me to say "run compliment.java" on a mouse click.

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with some coding

    Sorry... I realize it was a bit rushed...

    here's the code for the generator so far:

    import java.util.Random;
     
    public class Compliment {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Random myRandom = new Random();
    		int randomNumber;
     
    		randomNumber = myRandom .nextInt(10) + 1;
     
    		switch (randomNumber) {
    		case 1:
    		case 2:
    		case 3:
    		case 4:
    		case 5:
    		case 6:
    		case 7:
    		case 8:
    		case 9:
    		case 10:
    		default:
     
    		}
     
    	}
     
    }

    I want any output from this generator to be displayed in the text field next to the generate button which is set up with a action listener. What is the syntax needed to say "print to JTextPane" and what is the syntax needed to say "run compliment.class" on a mouse click

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with some coding

    I don't see any GUI code in what you have posted. Have you looked at the tutorial to see how to write some:
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with some coding

    (Sorry for posting twice... my post didn't show up right after posting... both times)
    This is not the code for the GUI... just the beginnings of my random compliment generator...

    This is the code for the GUI, I'm using window builder with eclipse to do it:

    import java.awt.EventQueue;
     
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import java.awt.BorderLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JTextArea;
    import javax.swing.JTextPane;
    import java.awt.TextField;
     
     
    public class myframe extends JFrame {
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					myframe frame = new myframe();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the frame.
    	 */
    	public myframe() {
    		setBounds(100, 100, 450, 300);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		getContentPane().setLayout(null);
     
    		JButton btnNewButton = new JButton("Video");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    			}
    		});
    		btnNewButton.setBounds(10, 11, 105, 23);
    		getContentPane().add(btnNewButton);
     
    		JButton btnNewButton_1 = new JButton("Information");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    			}
    		});
    		btnNewButton_1.setBounds(10, 45, 105, 23);
    		getContentPane().add(btnNewButton_1);
     
    		JButton btnNewButton_2 = new JButton("Generate");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    			}
    		});
    		btnNewButton_2.setBounds(10, 174, 105, 23);
    		getContentPane().add(btnNewButton_2);
     
    		JTextPane textPane = new JTextPane();
    		textPane.setBounds(125, 174, 299, 23);
    		getContentPane().add(textPane);
     
    	}
    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with some coding

    What are your questions or problems now?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with some coding

    Same questions... I want to run the compliment program using the "generate" button... and I want to display the output in the text pane next to it... simply put I don't know how to do it. the only syntax I know that displays output on the screen is "out.println" but that won't print the output in the text pane... at least I don't think it will. I want to know the syntax that designates the text pane as the area where I want the output printed.

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need help with some coding

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with some coding

    I apologize, but I'm just not getting it... I checked out the link you posted KevinWorkman, but I still can't find a way to do what I want to do. May it be because I'm approaching the program the wrong way?

    --- Update ---

    Oh! I just found something that may help me out. How do I redirect all "system" traffic to the jTextArea I have set up (i changed it from a jTextPane)? Like, when I type "system.out.println()" I want whatever it is to be printed in the jTextArea instead.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with some coding

    There is a method in the System class that will change the class of the out variable. You could change that to a class of yours that would append what was written to System.out to the text area.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with some coding

    Ok, I have System.setOut()... but what goes inside the parenthesis? I've tried "JTextPane", I've tried "JTextPane.class", I've tried "myframe.class". I just don't know the syntax required to say "this text pane in this class".

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need help with some coding

    Quote Originally Posted by Firearrow5235 View Post
    Ok, I have System.setOut()... but what goes inside the parenthesis? I've tried "JTextPane", I've tried "JTextPane.class", I've tried "myframe.class". I just don't know the syntax required to say "this text pane in this class".
    You can't just guess at the syntax. What type of Object does that method take? Do you have that type of Object available?

    You should also think about other approaches: are you sure you want to redirect your System.out? Take a look at the API for JTextPane for methods that let you set the text. Could you use that instead?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with some coding

    I don't know if I can use it... That's kinda why I'm here... I am an absolute beginner. I'm writing this program mainly as practice, and to find out what I need to learn.

    Here's my code so far...

    import static java.lang.System.out;
     
    import java.awt.EventQueue;
     
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import java.awt.BorderLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JTextArea;
    import javax.swing.JTextPane;
    import java.awt.TextField;
    import java.util.Random;
    import java.util.Random;
     
    public class myframe extends JFrame {
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					myframe frame = new myframe();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the frame.
    	 */
    	public myframe() {
    		setBounds(100, 100, 450, 300);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		getContentPane().setLayout(null);
     
    		JButton btnNewButton = new JButton("Video");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    			}
    		});
    		btnNewButton.setBounds(10, 11, 105, 23);
    		getContentPane().add(btnNewButton);
     
    		JButton btnNewButton_1 = new JButton("Information");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    			}
    		});
    		btnNewButton_1.setBounds(10, 45, 105, 23);
    		getContentPane().add(btnNewButton_1);
     
    		JButton btnNewButton_2 = new JButton("Generate");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				myframe.compliment();
    			}
    		});
    		btnNewButton_2.setBounds(10, 174, 105, 23);
    		getContentPane().add(btnNewButton_2);
     
    		JTextPane textPane = new JTextPane();
    		textPane.setBounds(125, 174, 299, 23);
    		getContentPane().add(textPane);
     
    }
     
    protected static void compliment() {
    		// TODO Auto-generated method stub
    	Random myRandom = new Random();	
    	int randomNumber; {
     
     
    	randomNumber = myRandom .nextInt(10) + 1;
     
    	switch (randomNumber) {
    	case 1:
    		out.println("Insert comment here!");
    		break;
    	case 2:
    		out.println("Insert comment here!");
    		break;
    	case 3:
    		out.println("Insert comment here!");
    		break;
    	case 4:
    		out.println("Insert comment here!");
    		break;
    	case 5:
    		out.println("Insert comment here!");
    		break;
    	case 6:
    		out.println("Insert comment here!");
    		break;
    	case 7:
    		out.println("Insert comment here!");
    		break;
    	case 8:
    		out.println("Insert comment here!");
    		break;
    	case 9:
    		out.println("Insert comment here!");
    		break;
    	case 10:
    		out.println("Insert comment here!");
    		break;
    	default:
    		out.println("Insert comment here!");
    		break;
    	}
    	}
    	}
    }

    Tell me what I need to do to get what I want.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with some coding

    TextPanes are much more complicated than textfields. I'd change to a textfield.

    If you want to trap System.out.println() using setOut() you need to read the API doc for setOut() and find what type of arg it takes. Then you need to work on creating that type of arg.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with some coding

    Ahhh! Ok... so it takes a printstream... I know how to create a printstream but what ties that printstream to the text pane? Do I just have to create the printstream in the arguments "section" of the textpane's code?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with some coding

    The PrintStream class you create gets the String printed by the print/println methods.
    You can then append that String to the textarea.
    If you don't understand my answer, don't ignore it, ask a question.

  18. The Following User Says Thank You to Norm For This Useful Post:

    Firearrow5235 (April 13th, 2013)

  19. #18
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with some coding

    OK... thank you for all your help!

Similar Threads

  1. [SOLVED] I need help with my coding
    By knockturnal22 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 16th, 2012, 04:19 PM
  2. Need Help With Coding
    By noles227 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 22nd, 2012, 07:35 PM
  3. Replies: 2
    Last Post: February 19th, 2012, 07:36 AM
  4. Need your help to coding
    By Tony_nguyen19 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 14th, 2011, 08:58 AM
  5. Help me in java coding
    By smallmac in forum Java Theory & Questions
    Replies: 5
    Last Post: August 2nd, 2010, 09:50 AM