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

    Default Textfile and GUI problems

    I have a problem where i have to call a textfile from Java. To call it i have used the buffered reader, but my problem is what do i have to write in the if condition, so that when you click on a button it will open the specified textfile. I'm making a quiz, and i want that when one clicks one of the buttons such as plate tectonics, it opens the textfile in a GUI window displaying a set of questions found in the textfile called platetectonics.txt. Can you help me pls? Thanks a lot.

    *This is the code i have written:*

    {code}
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;

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

    import java.util.Arrays;

    class Ch14FlowLayoutSample extends JFrame {





    private static final int FRAME_WIDTH = 95;
    private static final int FRAME_HEIGHT = 170;
    private static final int FRAME_X_ORIGIN = 70;
    private static final int FRAME_Y_ORIGIN = 50;






    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");


    Ch14FlowLayoutSample frame = new Ch14FlowLayoutSample();
    frame.setVisible(true);

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

    public Ch14FlowLayoutSample() {
    Container contentPane;
    JButton button1, button2, button3, button4, button5;


    setSize (FRAME_WIDTH, FRAME_HEIGHT);
    setTitle("Geography Quiz");
    setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);

    contentPane = getContentPane();
    contentPane.setBackground(Color.pink);
    contentPane.setLayout(new FlowLayout());

    button1 = new JButton("Plate Tectonics");
    button2 = new JButton("Rivers");
    button3 = new JButton("Rocks");
    button4 = new JButton("Quit");

    contentPane.add(button1);
    contentPane.add(button2);
    contentPane.add(button3);
    contentPane.add(button4);

    if (clickedbutton == button1)
    {
    Scanner s = null;
    try {
    s = new Scanner(new BufferedReader(new FileReader("plate_tectonics.txt")));

    while (s.hasNext()) {
    s.useDelimiter(",\\s*");
    JOptionPane.showInputDialog(null,s.nextLine());

    }
    } finally {
    if (s != null)
    s.close();

    }

    }
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }{code}

    *And this is the piece of code i'm having trouble with:-*

    {code}
    if (clickedbutton == button1)
    {
    Scanner s = null;
    try {
    s = new Scanner(new BufferedReader(new FileReader("plate_tectonics.txt")));

    while (s.hasNext()) {
    s.useDelimiter(",\\s*");
    JOptionPane.showInputDialog(null,s.nextLine());

    }
    } finally {
    if (s != null)
    s.close();

    }{code}

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    You need to implement an ActionListener for the event you wish to operate on. The key here is to override the abstract method actionPerformed() otherwise you'll get an abstract class but non-abstract method error. Make sure you use the try/catch properly by writing a catch statement.

    Go here for information on event listeners.


    Java Code:
    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 Ch14FlowLayoutSample extends JFrame [B]implements ActionListener[/B] {
    
      private static final int FRAME_WIDTH = 145;
      private static final int FRAME_HEIGHT = 170;
      private static final int FRAME_X_ORIGIN = 70;
      private static final int FRAME_Y_ORIGIN = 50;
    
      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, Password;
        String yourChoice, 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");
    
          Ch14FlowLayoutSample frame = new Ch14FlowLayoutSample();
          frame.setVisible(true);
    
        } else {
          JOptionPane.showMessageDialog(null, "Invalid Password. Try Again");
        }
      }
    
      public Ch14FlowLayoutSample() {
        Container contentPane;
        JButton button1, button2, button3, button4, button5;
    
        setSize (FRAME_WIDTH, FRAME_HEIGHT);
        setTitle("Geography Quiz");
        setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
    
        contentPane = getContentPane();
        contentPane.setBackground(Color.pink);
        contentPane.setLayout(new FlowLayout());
    
        button1 = new JButton("Plate Tectonics");
        button2 = new JButton("Rivers");
        button3 = new JButton("Rocks");
        button4 = new JButton("Quit");
    
        contentPane.add(button1);
        contentPane.add(button2);
        contentPane.add(button3);
        contentPane.add(button4);
        
    [B]     button1.addActionListener(this);
        button1.setActionCommand("b1");
        button4.addActionListener(this);
        button4.setActionCommand("b4");[/B]
    
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    
      } 
      
    [B]   public void actionPerformed(ActionEvent e) {[/B]
    
       [B] if ("b1".equals(e.getActionCommand())) {
        
    [/B]       Scanner s = null;
             try {
            s = new Scanner(new BufferedReader(new FileReader("plate_tectonics.txt")));
    
            while (s.hasNext()) {
              s.useDelimiter(",\\s*");
              JOptionPane.showInputDialog(null,s.nextLine());
            }[B]
          
          } catch (java.io.FileNotFoundException f) {
            JOptionPane.showMessageDialog(null, "File not found.");
        
          } [/B]finally {
            if (s != null)
              s.close();
          }[B]
        } else if ("b4".equals(e.getActionCommand())) {
            System.exit(0);
        
        }[/B]  
      [B]}[/B]
    }

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

    Default

    Ok thanks a lot. i really appreciate your help! Thanks again :-) You'r Great!

Similar Threads

  1. ECG plot in a textfile
    By samson in forum Java 2D
    Replies: 13
    Last Post: 01-28-2012, 05:02 PM
  2. Search TextFile
    By gsupriyarao@yahoo.com in forum Advanced Java
    Replies: 5
    Last Post: 04-11-2008, 12:03 PM
  3. reading textfile from java problem
    By saytri in forum New To Java
    Replies: 1
    Last Post: 01-17-2008, 03:13 AM
  4. textfile with Java problems
    By saytri in forum New To Java
    Replies: 4
    Last Post: 12-29-2007, 06:16 PM
  5. Problem with storing and retrieving from a textfile
    By Albert in forum Advanced Java
    Replies: 1
    Last Post: 07-13-2007, 04:01 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
  •