Results 1 to 2 of 2

Thread: GUI problems.

  1. #1
    saytri is offline Member
    Join Date
    Dec 2007
    Posts
    34
    Rep Power
    0

    Smile GUI problems.

    I have a problem where i need to display the contents of a textfile in a dialogue box. I'm making a quiz, and after the user enters the password and choose which quiz to take, java calls the required texfiles and opens it. I have managed to call the textfile through java by using the buffered reader, but i wish to display it in a GUI box. Can you pls help me? Thanks a lot!


    This is the code i've written:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;

    import java.io.*;
    import java.util.Scanner;

    import java.util.Arrays;

    public class GeographyQuiz_Menu
    {


    public static void main(String[] args)
    {
    JFrame jFrame;
    jFrame = new JFrame();



    JOptionPane.showMessageDialog(jFrame, "This is a Geography Quiz");
    JOptionPane.showMessageDialog(null, "Good Luck");



    char choice;
    int i, choice1;
    String yourChoice;
    int Password;
    String passString;



    passString = JOptionPane.showInputDialog("Enter the Password");


    //Password = passString.nextInt();
    Password = Integer.parseInt(passString);


    if (Password == 123)
    {
    JOptionPane.showMessageDialog(null, "Valid. You typed the right password. Now choose from the following menu");
    JOptionPane.showMessageDialog(null, "Welcome!\nThis is a Geography Quiz\nChoose from the following Menu:");


    JOptionPane.showMessageDialog(null, "1. Plate Tectonics\n2. Rivers\n3. Rocks\n4. Quit");

    passString = JOptionPane.showInputDialog("Enter your choice");
    choice1 = Integer.parseInt(passString);

    // Convert variable from string to integer
    if(choice1<1 || choice1>4)
    {
    JOptionPane.showMessageDialog(null, "This is an invalid choice");
    }

    switch(choice1)
    {
    case 1: JOptionPane.showMessageDialog(null,"You are taking the Plate Tectonics Quiz");
    Scanner s = null;
    try {
    s = new Scanner(new BufferedReader(new FileReader("plate_tectonics.txt")));

    while (s.hasNext()) {
    s.useDelimiter(",\\s*");
    JOptionPane.showMessageDialog(null,s.next());
    }
    } finally {
    if (s != null)
    s.close();

    break;

    }
    }

    }

    else
    {
    JOptionPane.showMessageDialog(null, "Invalid Password. Try Again");
    }

    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    A pseudo code suggestion:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    
    public class GQM
    {
        public static void main(String[] args)
        {
            JFrame jFrame;
            jFrame = new JFrame();
            JOptionPane.showMessageDialog(jFrame, "This is a Geography Quiz");
            JOptionPane.showMessageDialog(null, "Good Luck");
    
            char choice;
            int i, choice1;
            String yourChoice;
            int Password;
            String passString;
    
            passString = JOptionPane.showInputDialog("Enter the Password");
    
    //        Password = passString.nextInt();
            Password = Integer.parseInt(passString);
    
            if (Password == 123)
            {
                JOptionPane.showMessageDialog(null, "Valid. You typed the right password. " +
                                              "Now choose from the following menu");
                JOptionPane.showMessageDialog(null, "Welcome!\nThis is a Geography Quiz\n" +
                                              "Choose from the following Menu:");
    
    
                JOptionPane.showMessageDialog(null, "1. Plate Tectonics\n2. Rivers\n3. Rocks\n4. Quit");
    
                passString = JOptionPane.showInputDialog("Enter your choice");
                choice1 = Integer.parseInt(passString);
    
                // Convert variable from string to integer
                if(choice1<1 || choice1>4) 
                {
                    JOptionPane.showMessageDialog(null, "This is an invalid choice"); 
                }
    
                switch(choice1)
                {
                    case 1:
                        JOptionPane.showMessageDialog(null,"You are taking the Plate Tectonics Quiz");
                        String s = readFile("plate_tectonics.txt");
                        JOptionPane.showMessageDialog(null, s);
                        // Or you can write this into a JTextArea in your JFrame center section.
                }
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Invalid Password. Try Again");
            }
        }
    
        private static String readFile(String path)
        {
            Scanner s = null;
            StringBuilder sb = new StringBuilder();
            String separator = " ";  // or "\n"
            try
            {
                s = new Scanner(new BufferedReader(new FileReader(path)));
                s.useDelimiter(",\\s*");
                while (s.hasNext())
                {
                    sb.append(s.next(), separator);
    //                JOptionPane.showMessageDialog(null,s.next());
                }
            }
            finally
            {
                if (s != null) 
                    s.close();
                break;
            }
            return sb.toString();
        }
    }

Similar Threads

  1. Problems in JSP : Need help
    By raj4u in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-07-2008, 11:06 AM
  2. gui problems
    By bluebirdjc in forum Advanced Java
    Replies: 2
    Last Post: 07-23-2007, 06:38 PM
  3. a few problems
    By gary in forum AWT / Swing
    Replies: 0
    Last Post: 07-11-2007, 05:57 PM
  4. problems with JPA
    By Ed in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 06:34 AM
  5. Problems with the jar
    By Freddie in forum NetBeans
    Replies: 2
    Last Post: 05-29-2007, 04:50 PM

Posting Permissions

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