Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Fubarable

Thread: user input on gui

  1. #1
    JoePenguin is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default user input on gui

    i'm new to java and very new to GUIs. i would like to know how to define what the user has input into a JTextField. i would like to know how to wrte an if statement for the user input of the GUI so the program can answer back in the JTextArea.
    at the moment i have 3 classes, 1 creating the gui, 1 running the program and 1 with this JTextField and JTextArea which is added to to gui but it wont respond to the user input how i inteded it to.

    the code for the JTextfield and Area is this:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    import java.awt.event.*;
    public class play1 extends JPanel implements ActionListener{
    	JTextField textfield1 = new JTextField();
    	JTextArea textarea1 = new JTextArea();
    	private final static String newline = "\n";
    
    	    public play1(){
    		super(new GridBagLayout());
    	    
            textfield1 = new JTextField(20);
            textfield1.addActionListener(this);
     
            textarea1 = new JTextArea(10, 20);
            textarea1.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(textarea1);	
            
            GridBagConstraints c = new GridBagConstraints();
            c.gridwidth = GridBagConstraints.REMAINDER;
     
            add(textfield1, c);
     
            c.gridx = 0;
            c.gridy = -3;
            add(scrollPane, c);
        }
    	    public void actionPerformed(ActionEvent evt) {
    	        String text = textfield1.getText();
    	        textarea1.append(text + newline);
    	        	if(text.equals("hey"));{ textarea1.append("hello to you too" +newline);
    	 
    	        textfield1.selectAll();
    	}}}
    all i want to know is how to correctly get the if statement in.

    sorry if this was badly worded... extremly tired :/
    thankyou in advance :)
    Last edited by Fubarable; 01-26-2012 at 05:09 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: user input on gui

    Quote Originally Posted by JoePenguin View Post
    i'm new to java and very new to GUIs. i would like to know how to define what the user has input into a JTextField. i would like to know how to wrte an if statement for the user input of the GUI so the program can answer back in the JTextArea.
    at the moment i have 3 classes, 1 creating the gui, 1 running the program and 1 with this JTextField and JTextArea which is added to to gui but it wont respond to the user input how i inteded it to.
    Please clarify the problem so that we will better know what is wrong and how to help you. For instance, usually a user will enter text into a GUI and then notify the GUI when he is done and wishes the program to process his input. How are you planning to do this? With a JButton? Something else? Also, please explain any specific problems you're having with your code -- bugs, misbehavior, etc,

    all i want to know is how to correctly get the if statement in.
    What if statement where?

    sorry if this was badly worded... extremly tired :/
    All we ask is that you put as much effort into asking your question as you would desire one of the volunteers here to put in answering it.

    Best of luck.

    Also: I've added code tags to your original post to help the code to retain its formatting.

  3. #3
    Mr.Java is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Smile Re: user input on gui

    all i want to know is how to correctly get the if statement in.
    Java Code:
       	public void actionPerformed(ActionEvent evt) {
    		String text = textfield1.getText();
    		textarea1.append(text + newline);
    		
    		if(text.equals("hey")){ 
    			textarea1.append("hello to you too" +newline);
    			textfield1.selectAll();
    			//textfield1.setText("");
    		}
    		
           /**   else
    	*		textarea1.append(text + newline);
    	*     textfield1.setText("");
    	*/	
    	}
    Last edited by Mr.Java; 01-26-2012 at 06:17 AM. Reason: spellmistaks

  4. #4
    JoePenguin is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: user input on gui

    Please clarify the problem so that we will better know what is wrong and how to help you. For instance, usually a user will enter text into a GUI and then notify the GUI when he is done and wishes the program to process his input. How are you planning to do this? With a JButton? Something else? Also, please explain any specific problems you're having with your code -- bugs, misbehavior, etc,
    right, im now awake xD... the problem is that i would like my program to talk back to me. so if i say "hello" in the textfield, it will say hello back. if a word other than hello is inputted, i would like it to say something like "i dont understand".
    the if statement was this bit
    public void actionPerformed(ActionEvent evt) {
    String text = textfield1.getText();
    textarea1.append(text + newline);
    if(text.equals("hey"));{ textarea1.append("hello to you too" +newline);

    textfield1.selectAll();
    }}}
    i tried using 'else' but eclipse keeps saying "syntax error on "else", delete this token"...
    so i would like to know; how am i able to make my program talk back to me using if and else statements?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: user input on gui

    Your else must not be placed correctly, but without the offending code, it's difficult to say what the problem is. One thing that you can and in fact should do is format your code better. Often fixing the formatting, including using standard 3 space indentations, spaces between key works and parenthesis or braces, new lines where they belong, and precise proper placement of curly braces, will show you what is wrong with your if/else statements. For instance this has no place in your code:

    Java Code:
    // ??? way too much on one line
    if(text.equals("hey"));{ textarea1.append("hello to you too" +newline);
          
                textfield1.selectAll();
        }}} // ???? split these up.
    Use white space to its best advantage to allow you to see what the code logically is doing. If you in fact do this, you'll see a glaring problem right away:

    Java Code:
          if (text.equals("hey")); {   // ??? hey -- what's the semicolon doing there?
             textarea1.append("hello to you too" +newline);
    
             textfield1.selectAll();
          }
       }
    }

    So study a code formatting guide, use it, and it will prevent many stupid or careless errors (like the extra bad semi-colon above).
    JoePenguin likes this.

  6. #6
    JoePenguin is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

Similar Threads

  1. User Input???
    By jonytek in forum New To Java
    Replies: 8
    Last Post: 01-13-2013, 03:52 PM
  2. User input
    By the ole buc in forum New To Java
    Replies: 16
    Last Post: 12-11-2011, 08:08 PM
  3. Need help getting input(first/last name) from user
    By nightrise420 in forum New To Java
    Replies: 11
    Last Post: 09-11-2010, 04:09 AM
  4. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 02:59 AM
  5. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 05:47 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •