Results 1 to 6 of 6
Thread: user input on gui
- 01-26-2012, 03:46 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 7
- Rep Power
- 0
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:
all i want to know is how to correctly get the if statement in.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(); }}}
sorry if this was badly worded... extremly tired :/
thankyou in advance :)Last edited by Fubarable; 01-26-2012 at 04:09 AM. Reason: code tags added
-
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,
What if statement where?all i want to know is how to correctly get the if statement in.
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.sorry if this was badly worded... extremly tired :/
Best of luck.
Also: I've added code tags to your original post to help the code to retain its formatting.
- 01-26-2012, 05:06 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
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 05:17 AM. Reason: spellmistaks
- 01-26-2012, 03:26 PM #4
Member
- Join Date
- Jan 2012
- Posts
- 7
- Rep Power
- 0
Re: user input on gui
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".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,
the if statement was this bit
i tried using 'else' but eclipse keeps saying "syntax error on "else", delete this token"...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();
}}}
so i would like to know; how am i able to make my program talk back to me using if and else statements?
-
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:
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:// ??? way too much on one line if(text.equals("hey"));{ textarea1.append("hello to you too" +newline); textfield1.selectAll(); }}} // ???? split these up.
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).
- 01-26-2012, 07:27 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
User Input???
By jonytek in forum New To JavaReplies: 8Last Post: 01-13-2013, 02:52 PM -
User input
By the ole buc in forum New To JavaReplies: 16Last Post: 12-11-2011, 07:08 PM -
Need help getting input(first/last name) from user
By nightrise420 in forum New To JavaReplies: 11Last Post: 09-11-2010, 03:09 AM -
User Input
By brmcdani in forum New To JavaReplies: 2Last Post: 02-05-2010, 01:59 AM -
User input- Pop Up Box
By dedachi in forum AWT / SwingReplies: 3Last Post: 03-23-2009, 04:47 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks