Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-16-2007, 09:41 PM
Member
 
Join Date: Dec 2007
Posts: 34
saytri is on a distinguished road
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");
}

}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-16-2007, 11:27 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
A pseudo code suggestion:
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(); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 07:42 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org