Getting Text to show on GUI

I'm about desperate because i spent 3 days on this and i'm am in dire need for help!

1. First i want to create a GUI that it a quiz.

2. Create Questions by using Combo Box

3. Next it creates a text field and allow me to input my answer

4. following that, after the input is inputted, i click a button that says check answer and it will return if it is correct or false.

The problem is that code is so messed up that i don't know where to go.

When i run it, it show me a combo box with 3 questions but before i even answer it, it shows that i'm incorrect.

Thats why I can't really brief wat i need help in. So, could you write a quick code that can summarize the whole thing. You can use my code and fix it up. Thanks very so much!

package anaquiz;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass AnAQuizextends JPanel{

private JComboBox choice;

private JLabel label;

private JTextField text;

Button CheckAnswer;

Button TryAgain;

private String Answer1 ="Wilson";

private String Answer2 ="Ace";

private String Answer3 ="Yes";

private String input =" ";

boolean Answer =false;

JLabel testresult =new JLabel("");

public AnAQuiz(){

choice =new JComboBox();

label =new JLabel();

label.setBackground(Color.blue);

choice.addItem("Tennis Question #1");

choice.addItem("Tennis Question #2");

choice.addItem("Tennis Question #3");

text =new JTextField(20);//step 4

Listener listen =new Listener();

choice.addActionListener(listen);

setLayout(new BoxLayout(this, 0));

add(choice);

add(label);

add(text);

add(testresult);

}

privateclass Listenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent event ){

int c = choice.getSelectedIndex();

switch (c){

case 0:

label.setText("Whats the famous tennis brand that begins with 'W'?");

if (Answer1.equals(input))testresult.setText("Correct");

else{ testresult.setText("Incorrect");

}

break;

case 1:

label.setText("What do you call when someone misses a serve?");

if (Answer2.equals(input))testresult.setText("Correct");

else{ testresult.setText("Incorrect");

}

break;

case 2:

label.setText("Should you shake hands after a match?");

if (input.equals(Answer3))testresult.setText("Correct");

else{

testresult.setText("Incorrect");

}

break;

}

testresult.setVisible(false);

}

}

publicstaticvoid main(String[] args){

JFrame frame =new JFrame("Quiz");

frame.getContentPane().add(new AnAQuiz());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

// frame.show();

frame.setVisible(true);

}

}

[5580 byte] By [Vausta] at [2007-11-26 19:34:02]
# 1

> When i run it, it show me a combo box with 3 questions but before i even answer it, it shows that i'm incorrect.

Well, what do you expect to happen?

You added an ActionListener to the combo box to display the message. The code to check the answer is in the same place as the code to set the message so of course its executed at the same time.

Remove the code that checks the answer.

> i click a button that says check answer and it will return if it is correct or false

When you add your button, you will also need to add an ActionListener to handle the button click. So you have two choices:

a) create a separate ActionListener for the button and use the code that you just removed from above. This is the best choice.

b) check which component generated the ActionEvent (either the comboBox or the button).

If the comboBox generated the event then you update the question and clear the other fields.

If the button generated the event, then you check the answer to see if its valid.

Although this is not the preferred solution you can find an example of this approach by reading the Swing tuorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/button.html]How to Use Buttons[/url].

camickra at 2007-7-9 22:07:11 > top of Java-index,Desktop,Core GUI APIs...
# 2
Srry, i'm not very good at java yet so i don't really know where to put stuff. Could you arrange it for me plz?
Vausta at 2007-7-9 22:07:11 > top of Java-index,Desktop,Core GUI APIs...
# 3
> Could you arrange it for me plz? I just did. I said you have one ActionListener to set the question.And you have a second ActionListener to check the answer.
camickra at 2007-7-9 22:07:11 > top of Java-index,Desktop,Core GUI APIs...
# 4
hmmm let me try
Vausta at 2007-7-9 22:07:11 > top of Java-index,Desktop,Core GUI APIs...
# 5

Do you mean something like this?

I still have some small minor errors

package anaquiz;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class AnAQuiz extends JPanel {

private JComboBox choice;

private JLabel label;

private JTextField text;

Button CheckAnswer;

Button TryAgain;

private String Answer1 = "Wilson";

private String Answer2 = "Ace";

private String Answer3 = "Yes";

private String input = " ";

boolean Answer = false;

JLabel testresult = new JLabel("");

public AnAQuiz() {

choice = new JComboBox();

label = new JLabel();

label.setBackground(Color.blue);

choice.addItem("Tennis Question #1");

choice.addItem("Tennis Question #2");

choice.addItem("Tennis Question #3");

text = new JTextField(20); //step 4

Listener listen = new Listener();

choice.addActionListener(listen);

setLayout(new BoxLayout(this, 0));

add(choice);

add(label);

add(text);

add(testresult);

}

private class Listener implements ActionListener {

public void actionPerformed(ActionEvent event) {

int c = choice.getSelectedIndex();

switch (c) {

case 0:

label.setText(

"Whats the famous tennis brand that begins with 'W'?");

break;

case 1:

label.setText("What do you call when someone misses a serve?");

break;

case 2:

label.setText("Should you shake hands after a match?");

break;

testresult.setVisible(false);

}

}

private class Listener implements ActionListener {

private void actionPerformed(ActionEvent event) {

if (Answer1.equals(input)) testresult.setText("Correct");

else

testresult.setText("Incorrect");

break;

if (Answer2.equals(input)) testresult.setText("Correct");

else

testresult.setText("Incorrect");

break;

if (input.equals(Answer3)) testresult.setText("Correct");

else

testresult.setText("Incorrect");

break;

}

}

public static void main(String[] args) {

JFrame frame = new JFrame("Quiz");

frame.getContentPane().add(new AnAQuiz());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

// frame.show();

frame.setVisible(true);

}

} // <-the error'}' expected at line 93(93:6)

Message was edited by:

Vaust

Vausta at 2007-7-9 22:07:11 > top of Java-index,Desktop,Core GUI APIs...
# 6

> Do you mean something like this?

No. You need two Listeners, you can't define the same method twice in the same class. Right now you have a single class "Listener" that implements ActionListener.

You need to create two classes, lets say "CheckBoxListener" and "ButtonListener" that implements ActionListener. Each class will have its own actionPerformed() method.

camickra at 2007-7-9 22:07:11 > top of Java-index,Desktop,Core GUI APIs...
# 7

package anaquiz;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class AnAQuiz extends JPanel {

private JComboBox choice;

private JLabel label;

private JTextField text;

Button CheckAnswer;

Button TryAgain;

private String Answer1 = "Wilson";

private String Answer2 = "Ace";

private String Answer3 = "Yes";

private String input = " ";

boolean Answer = false;

JLabel testresult = new JLabel("");

public AnAQuiz() {

choice = new JComboBox();

label = new JLabel();

label.setBackground(Color.blue);

choice.addItem("Tennis Question #1");

choice.addItem("Tennis Question #2");

choice.addItem("Tennis Question #3");

text = new JTextField(20); //step 4

ButtonListener listen = new ButtonListener();

CheckBoxListener listen1 = new CheckBoxListener();

choice.addActionListener(listen);

choice.addActionListener(listen1);

setLayout(new BoxLayout(this, 0));

add(choice);

add(label);

add(text);

testresult.setVisible(false);

add(testresult);

}

private class CheckBoxListener implements ActionListener {

public void actionPerformed(ActionEvent event) {

int c = choice.getSelectedIndex();

switch (c) {

case 0:

label.setText(

"Whats the famous tennis brand that begins with 'W'?");

break;

case 1:

label.setText("What do you call when someone misses a serve?");

break;

case 2:

label.setText("Should you shake hands after a match?");

break;

}

}

}

private class ButtonListener implements ActionListener {

public void actionPerformed(ActionEvent event) {

int c = choice.getSelectedIndex();

if (Answer1.equals(input)) testresult.setText("Correct");

else

testresult.setText("Incorrect");

if (Answer2.equals(input)) testresult.setText("Correct");

else

testresult.setText("Incorrect");

if (input.equals(Answer3)) testresult.setText("Correct");

else

testresult.setText("Incorrect");

}

}

public static void main(String[] args) {

JFrame frame = new JFrame("Quiz");

frame.getContentPane().add(new AnAQuiz());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

// frame.show();

frame.setVisible(true);

}

}

now it has no errors but correct and incorrect doesn't show after i type in the answer in the textfield after hitting the enter button

Vausta at 2007-7-9 22:07:11 > top of Java-index,Desktop,Core GUI APIs...
# 8
Before we continue, i would like to say thank you for your time. I'm a student still trying to learn the basics of java. I appreciate your help once again. Please be leanent on me.
Vausta at 2007-7-9 22:07:11 > top of Java-index,Desktop,Core GUI APIs...
# 9
> after i type in the answer in the textfield after hitting the enter button You add the "ButtonListener" to the text field.Events are only generated on components that have focus.
camickra at 2007-7-9 22:07:11 > top of Java-index,Desktop,Core GUI APIs...
# 10
Srry, i'm a bit lost now. =(
Vausta at 2007-7-9 22:07:11 > top of Java-index,Desktop,Core GUI APIs...
# 11
> Srry, i'm a bit lost now. =( How did you add the "CheckBoxListener" to the check box?Well, you do the same thing for the "ButtonListener" and the text field.
camickra at 2007-7-9 22:07:11 > top of Java-index,Desktop,Core GUI APIs...
# 12
let me try
Vausta at 2007-7-9 22:07:11 > top of Java-index,Desktop,Core GUI APIs...
# 13

> How did you add the "CheckBoxListener" to the check

> box?

>

> Well, you do the same thing for the "ButtonListener"

> and the text field.

Well, i don't really know. Could you see the latest code above to see if i'm on the rite track first of all.

Curious, how much longer could you be on? And could i get your email so i can email if i need help when you are not on?

Vausta at 2007-7-9 22:07:11 > top of Java-index,Desktop,Core GUI APIs...
# 14

I know that you may be busy with other stuff but if you could write a code to fill the requirements would be the best. I could refrence yours and see where i did wrong. That may actually save a bit time instead of you asking me questions and me not knowing wat i'm doing. Since i kinda gave you the layout, feel free to edit. I wish i could sent you 10 dollars for your help. I will though, definetly give you the 10 duke stars.

Vausta at 2007-7-9 22:07:11 > top of Java-index,Desktop,Core GUI APIs...
# 15
help is still needed. anyone plz!
Vausta at 2007-7-21 17:37:44 > top of Java-index,Desktop,Core GUI APIs...
# 16

> Well, i don't really know.

What do you mean you don't know. You wrote the code didn't you?

choice.addActionListener(listen);

choice.addActionListener(listen1);

Why did you add two listeners to the check box? You have two separate actions:

a) display the question to be asked

b) validate the answer given

Each listener needs to be added to a different component, since different components initiate the action:

text.addActionListener(listen);

choice.addActionListener(listen1);

Now when you use the "Enter" key and focus is on the text field the ActionListener added to the text field will execute causing the answer to be validated.

camickra at 2007-7-21 17:37:44 > top of Java-index,Desktop,Core GUI APIs...
# 17
let me go see wat i can do
Vausta at 2007-7-21 17:37:44 > top of Java-index,Desktop,Core GUI APIs...
# 18

Do you mean something like this?

package anaquiz;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class AnAQuiz extends JPanel {

private JComboBox choice;

private JLabel label;

private JTextField text;

Button CheckAnswer;

Button TryAgain;

private String Answer1 = "Wilson";

private String Answer2 = "Ace";

private String Answer3 = "Yes";

private String input = " ";

boolean Answer = false;

JLabel testresult = new JLabel("");

public AnAQuiz() {

ButtonListener listen = new ButtonListener();

CheckBoxListener listen1 = new CheckBoxListener();

text = new JTextField(20);

choice = new JComboBox();

label = new JLabel();

label.setBackground(Color.blue);

choice.addItem("Tennis Question #1");

text.addActionListener(listen);

choice.addActionListener(listen1);

choice.addItem("Tennis Question #2");

text.addActionListener(listen);

choice.addActionListener(listen1);

choice.addItem("Tennis Question #3");

text.addActionListener(listen);

choice.addActionListener(listen1);

text = new JTextField(20);

setLayout(new BoxLayout(this, 0));

add(choice);

add(label);

add(text);

testresult.setVisible(false);

add(testresult);

}

private class CheckBoxListener implements ActionListener {

public void actionPerformed(ActionEvent event) {

int c = choice.getSelectedIndex();

switch (c) {

case 0:

label.setText(

"Whats the famous tennis brand that begins with 'W'?");

break;

case 1:

label.setText("What do you call when someone misses a serve?");

break;

case 2:

label.setText("Should you shake hands after a match?");

break;

}

}

}

private class ButtonListener implements ActionListener {

public void actionPerformed(ActionEvent event) {

int c = choice.getSelectedIndex();

if (Answer1.equals(input)) testresult.setText("Correct");

else

testresult.setText("Incorrect");

if (Answer2.equals(input)) testresult.setText("Correct");

else

testresult.setText("Incorrect");

if (input.equals(Answer3)) testresult.setText("Correct");

else

testresult.setText("Incorrect");

}

}

public static void main(String[] args) {

JFrame frame = new JFrame("Quiz");

frame.getContentPane().add(new AnAQuiz());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

// frame.show();

frame.setVisible(true);

}

}

It still doesn't tell me if i'm correct or incorrect

Vausta at 2007-7-21 17:37:44 > top of Java-index,Desktop,Core GUI APIs...
# 19

Why do you add the ActionListener 3 times to each component?

Why do you create two text fields?

Why is your result field invisible?

Why doesn't your ActionListener for the text field have a "case" statement? You only want to validate an given answer for the questions that was asked.

Learn to develop some debugging skills. Have you added any System.out.printl(...) statements to your code to see whats happening?

camickra at 2007-7-21 17:37:44 > top of Java-index,Desktop,Core GUI APIs...
# 20

arrgghhhh. I'm having mad difficulties atm. i don't know. =( I give up. Thanks for your help btw. I still think that if you could write up a quick code would be really nice. That way i can refrence and learn from wat i did wrong. Your asking a person who only started java half a year ago. x_x I appreciate your help once again. Thank you for being really patient with me!

Vausta at 2007-7-21 17:37:44 > top of Java-index,Desktop,Core GUI APIs...
# 21
I will be away from the computer in a while because i have to go to chinese school. ttyl
Vausta at 2007-7-21 17:37:44 > top of Java-index,Desktop,Core GUI APIs...
# 22

> I still think that if you could write up a quick code would be really nice.

I gave you the code. I showed you that you only needed to change one line of code. For some reason you duplicated the code 3 times.

I told you you defined a variable twice. Does it not make sense that you would delete the second occurance?

These are basic concepts. If you don't understand them, sorry, but I can't teach them to you.

If you want examples then read the Swing tutorial. I've already pointed you to the tutorial once. Read the table of contents. There is a section on "How to Write an Action Listener". There are also examples of using every Swing component. You just have to put it together yourself.

camickra at 2007-7-21 17:37:44 > top of Java-index,Desktop,Core GUI APIs...