Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Java program to read file GUI?

I need to do the following:

The first button should be labeled "Open". When clicked, it should ask the user for a file name using a JOptionPane. It should then open the file by creating a FileReader and a Scanner.

ii. If the file opens successfully, the contents of the file should be read a line at a time using the Scanner object in a loop. Each line should be appended to the JTextArea (after clearing out the existing contents of the text area first). The title of the Frame should be set to the name of the file if successful.

iii. If the file does not exist, the program should not end - but the contents of the text area should be cleared and the title of the frame should be changed back to "My Reader"

iv. The second button should be labeled "Clear". When clicked, it should clear the textArea (i.e. set the text to ""). It should also rename the frame title back to " My Reader"

the clear button works fine as is, but the name of super will change in the previous operation, so dont know how to change it back.

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextArea;

public class ViewerFrame extends JFrame {

private JButton b1,b2;

private JTextArea field;

private JPanel panel, panel2;

public ViewerFrame(){

super("My Reader");

b1 = new JButton("Open");

b2 = new JButton("Clear");

field = new JTextArea(25,25);

panel = new JPanel();

panel2 = new JPanel();

panel.add(b1);

panel.add(b2);

panel2.add(field,BorderLayout.CENTER);

add(panel, BorderLayout.NORTH);

add(panel2, BorderLayout.CENTER);

b1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) {

Object source = event.getSource();

if (source == b1)

field.setText( "Fred was clicked");

}

}

);

b2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) {

Object source = event.getSource();

if (source == b2)

field.setText( " Pat was clicked");

}

}

);

}

}

import javax.swing.JFrame;

public class ViewerFrameTester {

public static void main(String[] args) {

ViewerFrame buck = new ViewerFrame();

buck.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

buck.setSize(600, 400);

buck.setVisible(true);

}

}

Update:

ive got the joptionpane to be working in the first actionlistener so it asks for file name

String input = JOptionPane.showInputDialog(null, "Enter Input:", "Dialog for Input",

JOptionPane.QUESTION_MESSAGE);

field.setText( input); }

Update 2:

im getting error at word exception. and if I get exception i want to set the title back to my reader, how to do?

b1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event)throws exception {

Object source = event.getSource();

if (source == b1)

{

String input = JOptionPane.showInputDialog(null, "Enter Input:", "Dialog for Input",

JOptionPane.QUESTION_MESSAGE);

setTitle(input);

FileReader fileR = new FileReader(input);

Scanner scan = new Scanner(fileR);

while (scan.hasNextLine() )

{

field.append(scan.nextLine());

}

field.setText(input); }

}

}

);

4 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    By the way, you are getting the error at the word exception because it should start with a capital "E", and to catch the Exception, use a try catch:

    try {

    //use code that throws an Exception

    } catch (Exception ex) {

    //If this code is run, an error happened, so set the

    //title here

    }

    You are probably better off using a JFileChooser. Here is a quick example I made.

    import java.awt.Insets;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileNotFoundException;

    import java.util.Scanner;

    import javax.swing.JButton;

    import javax.swing.JFileChooser;

    import javax.swing.JFrame;

    import javax.swing.JScrollPane;

    import javax.swing.JTextArea;

    public class SimpleFileOpen implements ActionListener {

    private JFrame frame;

    private JScrollPane scroll;

    private JTextArea txt;

    private JButton open, clear;

    public SimpleFileOpen() {

    txt = new JTextArea();

    scroll = new JScrollPane();

    scroll.setViewportView(txt);

    scroll.setBounds(0, 0, 300, 300);

    open = new JButton("Open");

    open.setBounds(0, 305, 145, 50);

    open.addActionListener(this);

    clear = new JButton("Clear");

    clear.setBounds(150, 305, 145, 50);

    clear.addActionListener(this);

    frame = new JFrame();

    frame.getContentPane().setLayout(null);

    frame.getContentPane().add(scroll);

    frame.getContentPane().add(open);

    frame.getContentPane().add(clear);

    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    frame.setVisible(true);

    Insets ins = frame.getInsets();

    frame.setSize(300 + ins.left + ins.right, 355 + ins.top + ins.bottom);

    }

    private void open() {

    JFileChooser fc = new JFileChooser();

    int option = fc.showOpenDialog(frame);

    if (option == JFileChooser.APPROVE_OPTION) { //FILE SELECTED

    //CLEAR SO YOU CAN APPEND FILE INFO TO TXT AREA

    clear();

    File file = fc.getSelectedFile();

    if (file.exists()) { //SELECTED FILE EXISTS

    try {

    Scanner s = new Scanner(new FileInputStream(file));

    while (s.hasNext()) { //WHILE THERE IS TXT IN FILE

    //APPEND THAT TEXT TO OUR TEXT AREA

    //(don't forget to add a new line)

    txt.append(s.nextLine() + "\n");

    }

    //FILE READ WITH NO ERROR, CHANGE TITLE

    frame.setTitle(file.getName());

    } catch (FileNotFoundException ex) {

    //ERROR WHILE READING FILE, CLEAR INSTEAD

    clear();

    }

    }

    }

    }

    private void clear() {

    txt.setText(null);

    frame.setTitle("My Reader");

    }

    public void actionPerformed(ActionEvent e) {

    if (e.getSource() == open) //OPEN CHOSEN

    open();

    else if (e.getSource() == clear)

    clear();

    }

    public static void main(String[] args) {

    new SimpleFileOpen();

    }

    }

  • Anonymous
    4 years ago

    1

    Source(s): Help Your Baby to Read http://emuy.info/ChildrenLearningReading
  • 1 decade ago

    Yeah, don't think you want to use super("name of title here"). What that actually does is it creates an entirely new frame. What you want is the setTitle method of the frame class to change the title to whatever:

    super.setTitle("name here");

    and use that whenever you have to change the name of the title.

    and then to use FileReader and Scanner you do this:

    FileReader fileR = new FileReader("name of file here");

    Scanner scan = new Scanner(fileR);

    and then you can use all the scanner methods to read from the file. Since it looks like you're just setting the text from the file to the text area, you can just do something like this (make sure to clear out the text area before you do this loop:

    while (scan.hasNextLine() )

    {

    field.append(scan.nextLine());

    }

    and that will fill your entire text area up, line for line, with file's contents.

  • 5 years ago

    If you should be exploring a fruitful way to greatly help your youngster learn to read while preventing any possible traps in the act then the program https://tr.im/PXKX5 is what you need.

    Children Learning Reading program is split in to two pieces which are generally provided in split up eBook s. Having bought the class you are provided immediate usage of each of the eBook s which means you are able to utilize the plan nearly straight away following investing in it.

    Children Learning Reading is user friendly for you and for your youngster, with small session and games this program is perfect to show how to read for a small child.

Still have questions? Get your answers by asking now.