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.*;
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'?");
                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);
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);
}

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

Similar Messages

  • Cant Get Text To Show In JTextArea

    Hi guys,
    I'm trying to create a very simplistic GUI where the output of an IRC server is shown in a JTextArea. However for the life of me I can not get any text at all to show in the text area. Here's the class that creates the user interface:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.io.*;
    public class SwingTest extends javax.swing.JFrame implements ActionListener {
         JButton connect = new JButton("Connect");
         JTextArea chat = new JTextArea();
        public SwingTest() {
             //set the title of the frame
             super("Chat Window");
             //set the size of the frame
             setSize(800, 600);
             //set what happens when the close button is clicked
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             // create the scroll pane to add the text area to
             chat.setLineWrap(true);
             JScrollPane scroll = new JScrollPane(chat, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
             //create the container panel
             JPanel jp = new JPanel();
             //create the layout manager and assign it to the container panel
             BoxLayout horizontal = new BoxLayout(jp, BoxLayout.Y_AXIS);
             jp.setLayout(horizontal);
             //add the text area and button to the container panel
             jp.add(scroll);
             jp.add(connect);
             //add the container panel to the frame
             add(jp);
             //make the frame and its contents visible
             setVisible(true);
             //add an event listener to the connect button
             connect.addActionListener(this);
        public void actionPerformed(ActionEvent event) {
             int temp = 1;
             SockThread mySock = new SockThread();
                 try {
                      Thread.sleep(20000);
                  while(temp == 1) {
                   while(mySock.pinger != false) {
                        chat.append(mySock.getStatus()+ "\n");
                        mySock.pinger = false;
                        break;
                 } catch  (InterruptedException ie) {
                      chat.setText("Error " + ie.getMessage());
        public static void main(String[] arguments)  {
             //create an instance of the SwingTest class and let it do its thing
             SwingTest st = new SwingTest();
    }

    Thanks hiwa, i did what you suggested and created a worker thread, but still I'm getting nothing in that JTextArea. I'm new to working with worker threads though, so if anyone wouldn't taking a look at my code just to see if I'm doing something wrong i would greatly appreciate it.
    Here is my worker class:
    import javax.swing.*;
    public class ChatWorker extends SwingWorker {
         String messages;
         String output;
         JTextArea chat;
         public ChatWorker() {
              super();
         protected String doInBackground() {
              int temp = 1;
              SockThread st = new SockThread();
              try {
                   Thread.sleep(20000);
                   //Perpetual loop needed to keep checking for server messages
                   while(temp == 1) {
                   while(st.pinger != false) {
                        messages = st.getStatus();
                        st.pinger = false;
                        break;
              } catch (InterruptedException ie) {
              return messages;
    }And here is the update gui class to make use of the worker thread:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.io.*;
    import java.beans.*;
    public class SwingTest extends javax.swing.JFrame implements ActionListener, PropertyChangeListener {
         JButton connect = new JButton("Connect");
         JTextArea chat = new JTextArea();
         ChatWorker cw;
        public SwingTest() {
             //set the title of the frame
             super("Chat Window");
             //set the size of the frame
             setSize(800, 600);
             //set what happens when the close button is clicked
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             // create the scroll pane to add the text area to
             chat.setLineWrap(true);
             JScrollPane scroll = new JScrollPane(chat, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
             //create the container panel
             JPanel jp = new JPanel();
             //create the layout manager and assign it to the container panel
             BoxLayout horizontal = new BoxLayout(jp, BoxLayout.Y_AXIS);
             jp.setLayout(horizontal);
             //add the text area and button to the container panel
             jp.add(scroll);
             jp.add(connect);
             //add the container panel to the frame
             add(jp);
             //make the frame and its contents visible
             setVisible(true);
             //add an event listener to the connect button
             connect.addActionListener(this);
        public void actionPerformed(ActionEvent event) {
             try {
             cw = new ChatWorker();
             cw.addPropertyChangeListener(this);
             cw.execute();
             } catch (Exception exc) {
        public void propertyChange(PropertyChangeEvent event) {
             try {
                  String tempText = (String)cw.get();
                  chat.append(tempText + "\n");
             }catch (Exception exc) {
        public static void main(String[] arguments)  {
             //create an instance of the SwingTest class and let it do its thing
             SwingTest st = new SwingTest();
    }

  • Getting text to show as hyperlink to navigate to next page

    Im making a website ( first time in visual studios) and its an online library. I have a book info page which shows more information about the book. The problem is that when i search for books how can i make it possible for the book to have a hyperlink
    to navigate towards the book info page. What i am doing right now is using a drop down list and i have a value assigned to each book(1-n) and have the person choose the number of the book and then click enter which navigates to the books more info page. I
    just want to be able to click on the title and navigate straight to it. 
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    namespace BookStoreOnline
    public partial class WebForm2 : System.Web.UI.Page
    private static string test;
    private static string testInfo;
    private static List<Book> resultList;
    private static List<Image> imageList;
    protected void Page_Load(object sender, EventArgs e)
    User.Text += (Session["UserName"]+"");
    protected void Button1_Click(object sender, EventArgs e)
    // if box is filled
    // check criteria selected
    // initiate search based on criteria
    // display results
    if (SearchBox.Text != "")
    resultList = new List<Book>();
    test = SearchCriteria.SelectedItem.Text;
    testInfo = SearchBox.Text;
    switch (test)
    case "ISBN":
    resultList = ((Database)Session["Database"]).SearchISBN(testInfo);
    break;
    case "Title":
    resultList = ((Database)Session["Database"]).SearchTitle(testInfo);
    break;
    case "Author":
    resultList = ((Database)Session["Database"]).SearchAuthor(testInfo);
    break;
    case "Semester":
    resultList = ((Database)Session["Database"]).SearchSemester(testInfo);
    break;
    case "Course":
    resultList = ((Database)Session["Database"]).SearchCourse(testInfo);
    break;
    case "Section":
    resultList = ((Database)Session["Database"]).SearchSection(testInfo);
    break;
    case "Professor":
    resultList = ((Database)Session["Database"]).SearchProfessor(testInfo);
    break;
    case "CRN":
    resultList = ((Database)Session["Database"]).SearchCRN(testInfo);
    break;
    if (resultList.Count == 0)
    NoResults.Text = "No results were found.";
    else
    Results.Text = "";
    for (int i = 0; i < resultList.Count; i++)
    int j = i + 1;
    ListItem bookIndex = new ListItem(j.ToString());
    Results.Text += "<pre>" + j + ". Title: " + resultList[i].Title + "<br>" + "Author: " + resultList[i].Author + "<br><br>" + "</pre>";
    MoreInfoIndex.Items.Add(bookIndex);
    //MoreInfoIndex.Items.Add(bookIndex); is what i am using to navigate to the book info page
    protected void MoreInfoButton_Click(object sender, EventArgs e)
    int k = Convert.ToInt32(MoreInfoIndex.SelectedItem.Text) - 1;
    Session.Add("Book", resultList[k]);
    //Session["Book"] = resultList[k];
    Response.Redirect("BookInfo.aspx");

    Please post questions related to ASP.NET in the ASP.NET forums (http://forums.asp.net ).

  • No matter what I have tried I still can't get text to show in the webpages

    Outside of Safari
    HP Pavilion zv6000   Windows XP   AMD 64 2.0ghz 2 gig ram 100 gig HD 128 meg video DLdvd+rw
    HP Pavilion zv6000   Windows XP   AMD 64 2.0ghz 2 gig ram 100 gig HD 128 meg video DLdvd+rw

    http://grupenet.com/2007/06/12/fix-font-issue-in-safari-for-windows/
    Try the solution on the above page in which the fonts.plist file has tobe downloade and replaced...

  • Getting Text onto One TV in the Objects Tab

    Hey All,
    I'm trying to put some text onto one tv in the objects tab in Live Type and
    I can't even get text to show up on one of them. In the users manual it only
    tells you how to get the tv onto a track but not how to get text onto the 'screen'.
    Is it possible to get text onto one screen?
    Thanks
    Ted

    For this effect I would suggest the following:
    Put the TV (like the horizontal TV slide) on the bottom track
    Put a text track on top of that
    Add the "TV Off" Effect to the text track...found in the "Mechanical" Category
    with the effect highlighted click on the "Timing" tab in the Inspector and in the "Speed" box change it from the default "reverse" to "forward"
    Adjust the positioning on your timeline to coincide with the TV motion
    It will now look like it's part of the TV

  • How do I get Alt text to show in DreamweaverCC

    Please excuse my ignorance. I would like to know how to get the Alt text to show when hovering over images. It use to happen naturally in Dreamweaver 3, but now in the latest CC version it doesn't seem to work. I use it to link images to pages so when some one hovers over an image the finger points but no text appears? Is there a setting I have missed as I can't type in the code as I'm a novice? Any advice would be appreciated. The pages are all htm.

    Hi Atrforrooms,
    Use Image Property Inspector dialog in Dreamweaver CC. There is title field, whose value will be displayed on hovering over the image.
    Below is the snapshot for better understanding and title is highlighted there. Try this and let us know if that worked for you.
    Wish you luck.
    thanks,
    lnayal

  • I downloaded the ios6 to my iphone and my moms phone  Now they are syncronized and I lost my contacts. All my phone numbers have emails in them but no phone numbers. Now whenever I get or send a text it shows up on her phone. How do I reverse it?

    I downloaded the ios6 update for the iphone to my phone and my moms"s phone. Now they are syncronized and I lost my contact telephone numbers. Now when I get a text or send  text it shows up on my mom's phone. I asked for help from the people at the ATT store and had no idea how to help me. That is crazy that they don't even know anything about their own products! This is EXTREMELY frustrating. Did you create a software that cannot be reversed or corrected? How to I restore my phone to the way it was? I am being told it can't be done. I have been an ATT customer for 8 years but I would love to go to Verizon right now. Pleae let me know how to reverse this and get the phones unsynced.

    If you share an Apple ID, both of your phone numbers should be linked to that ID. You should be able to use your phone number as well. My wife, my daughter and I all share an Apple ID for purchases, and we each text from our iOS devices using our own iPhone numbers.
    When iOS 6 came out, you were able to link the phone number with your Apple ID to use for Messages and FaceTime. You will need to sign out of messages on all devices and then start the process all over again.
    Take a look at this.
    iOS and OS X: Link your phone number and Apple ID for use - Support

  • Getting text messages to show up on all apple devices

    Anyone have any solutions? I have an iphone 5 (ios 7.1.1) iPad air (same) Macbook Pro (osx 10.9.3).  I am watching the WWC from Apple, and "Continuity" is going to solve this problem.  But, in the mean time is there any ways to get text messages to show up on multiple devices.

    yes, on each device go to
    settings>Messages>Send&Receive and select appropriately.
    Remember that this only appliesx to messages from the messages app, you cannot sync sms/mms messages received on an iphone.

  • All the sudden I can't get text form my daughters phone (I phone) she can get mine. Her phone no longer shows as an iPhone when I text her.

    I all the sudden can't get text form my daughter. She can get mine . When I text her it no longer comes up in blue like iPhone to iPhone does.

    I just had this issue today with a company phone and it was fixed by a reset: Hold down the home button along with the sleep/wake button until you see the apple, then let go.

  • HT3529 my iphone just stop connecting to my wifi and i have tried all the recommended troubleshooting with no success. Also when i send a text , it shows it coming from my email address instead of my cell #. i cant get a text going to my cell #. help

    my iphone just stopped connecting to my wifi and i have tried all the recommended troubleshooting with no success. Also when i send a text , it shows it coming from my email address instead of my cell #.  i dont receive any text going to my cell #. help

    If i understand what you are saying then you are seeing the WiFi indicator after connecting but Apps are saying that they are not connected to the internet?
    Your router may not have given your iPod a valid IP address. Go to Settings > Wifi > your network name and touch the ">" to the right to see the network details. If the IP address starts with 169 or is blank then your router didn't provide an IP address and you won't be able to access the Internet.
    Sometimes the fix can be to restart your router (remove power for 30 seconds and restart). Next, reset network settings on your iPod (Settings > General > Reset > Reset network settings) and then attempt to connect. In other cases it might be necessary to update the router's firmware with the latest from the manufacturer's support web pages.
    If you need more help please give more details on your network, i.e., your router make, model and version, the wifi security being used (WEP, WPA, WPA2), etc.

  • T42 (Type 2378) Rescue and Recovery text not showing up in GUI

    I have a machine that I hit the IBM button to restore my system back to its factory settings.  It loads up fine but when I hit the [Restore your system] it pulls up a new window and has 3 bullets I can select from, but there is NO text on the window.  Not even the < Back or Next > or Cancel button texts are showing up. I need to restore it but I don't know which bullet to select and if I select a random one, the next screen pulls up but still no text.  Any ideas?

    Thank you! I had to change my fast preview settings. I chose OpenGL Active instead of Wired Frame. Although, I'll have to play around with it because I got an error message last night that said it caused the program to crash.

  • Im getting texts from some of my contacts that no longer show their name but come across with  1 followed by the phone number.

    after updating my iphone 5 c to the latest iOS version, i am getting texts from some (not all) of my contacts that no longer display the name but only +1 followed buy the phone number. Is there something that needs to be adjusted in the settings to fix ths problem?

    Open your contact list and swipe down from the top. You can filter your contact list by account. Anything not in any of the listed contact types is a local contact. It would help if we could narrow down where the contacts were stored previously vs. where they are stored now.

  • How do I get text to flow from one page to the next in Pages 5?

    How do I get text to flow from one page to the next in Pages 5 with the Maverick system?

    What may be overlooked in Pages v5 is the notion of combining Text Boxes via the Menu > View > Show Arrange Tools. When you select two Text Boxes, an extra panel unfolds at the bottom of the Arrange Tools window. Uncheck one Text Box and this panel abruptly disappears.
    One can achieve text flow effects between Text Boxes differently based on how one positions the Text Box overlap, and choice of effect above. Though not the accustomed flow found in Pages ’09 v4.3, flow does occur. Consider the possibilities of combining a Shape and a Text box to cut an irregular Text box.
    If I create two Text Boxes and position them side by side with outline touching and choose Unite, this creates one larger Text Box and text flows across and down. On the otherhand, if I overlap the upper left corner of a lower Text Box over one above and to the left, then choose Union, pasted text fills the first Text Box and then flows across and down into the other box. Here is a Union example:
    Intersect will leave a small Text Box where the two overlap, so not much value there. Subtract will cut a chunk out of one Text Box that is the size of the overlapping piece of the second Text Box:
    And Intersect will leave an island in the center where the two Text Box overlap, with flow jumping over this bridge.
    Clearly, this is not what we were accustomed to in the past, but with imagination, style, and layout tuning, it does offer alternative Text Box creativity and layout. Of course, there is the option of simply returning to the previous Pages version for true Text Box linking that most will want to use.

  • I have 2 phones on my iTunes account, why are text messages showing on the second phone when sent from the first

    I have 2 phones on my iTunes account, why are text messages showing on the second phone when sent from the first phone?

    it's meant to work like that
    so if you receive a message on your iphone you also get it on your ipad or ipod touch
    to avoid it use separate appleID for each device
    or turn off imessage on 1 or both devices in their settings
    but that will not fix the issue that 2 devices using the same appleID will never be able to facetime eachother
    the appleID is a unique handle for 1 user only

  • Iphone 4s-when wifi is off and I am away from my home connection can I receive and send texts and text messages?  I want to keep my wifi off to limit data (email etc) to when I want to check it.  Cellular is roaming"voice only" it says.Can I get texts?

    When my wifi is off and I'm away from my home connection - it shows wifi off (unless I turn it on to check my email etc.) - and in the settings cellular it shows "voice only". Will I be able to receive and send texts in those settings?  Or check voicemail.  I had wanted to keep my email from coming in all the time and only want to check it periodically - so I turn ON my wifi when I want to check my email.  But when my wifi was off away from home- I hadn't seemed to be getting my texts.  So- do I need to keep wifi roaming connected when away from home to receive and send my texts in real time.  (which is what I want) or am I still able to send and receive texts with my wifi off when I'm away from home.  (When I'm home I am connected to my home data plan automatically through wifi and I get texts when I'm home - but I'm thinking I'm not getting texts in real time - is it because my wifi is off when I'm away from home?  My cellular shows voice only when my wifi is off away from home.  Am I doing it right to receive and send texts away from home?  Or do I need to open up the wifi in order to receive and send text messages.  I know that I would need to turn on wifi roaming - which will use data - for email checking or sending - as well as to use browsing apps and now I'm guessing to receive voice messages I may have missed?  So do I need wifi on to receive texts too?  Thank you - GregorySC

    Hello GregorySC,
    In order to send and receive SMS or MMS messages, you will need to have an active cellular data or Wi-Fi connection.
    You will need these to send and receive iMessages:
    An iPhone, iPad, or iPod touch
    iOS 5.0 or later
    A cellular data connection or a Wi-Fi connection
    A phone number or Apple ID registered with iMessage in Settings > Messages
    iOS: Troubleshooting Messages
    http://support.apple.com/kb/TS2755
    Cheers,
    Allen

Maybe you are looking for

  • Pacman -Syu today...Kernel2.6.5-2 and XFree86-4.4.0-1

       Attempted the upgrade and received query....remove ttf-bitstream-vera?    Answered Yes.   Prceeded with upgrade.   Loading of XFree86-4.4.0-1 encountered conflicts.    Is it again necessary to remove XFree86 before the upgrade and install it after

  • How to create startup disk to defray

    I need to defrag. I have iDefrag software on a USB drive. When I use the Snow Leapoard install disk to start up, I cant see the USB drive where iDefrag rests. Can someone please advise.

  • DEFAULT_WS_DEST web service destination - problem with production server

    Hi All, I'm getting close to go live for a project which uses a vast number of custom web services to access an Oracle DB backend to supply data to a Portal front end.  Many of the applications we have developed are based on Web Dynpro and so we have

  • OSB - x509 token handler

    Hello, I'm trying to develop a webservice with some policies associated: @Policies( { @Policy(uri = "policy:Wssp1.2-2007-EncryptBody.xml"), @Policy(uri = "policy:Wssp1.2-2007-SignBody.xml"), @Policy(uri = "policy:Wssp1.2-2007-Wss1.1-UsernameToken-Pla

  • How to store and use coordinates

    I need to store 2d coordinates and use them in solving equations and pass them into methods. For example, I need to store the coordinates of a corner of a rectangle (5,10). Then I need to use that point and solve for certain variables in a equation (