I am trying to open a file in a JTextArea and then write and read to it. I finally got it to open in the JTextArea with the FileReader and then broke it trying to incorporate FileWriter. Now I can't get it to open in the text area again. I have seen examples that show FileChooser opening a specific file but I want the user to be able to be able to pass a variable so that the user can use a FileChooser to open the any file they browser to. When I broke the code, I was adding a file reader to my OpenLister method. Is it common practice to put the FileReader and FileWriter in the same ActionListener? Any direction to a good example and or advise will be much appreciated. I have copied the code below.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class ClassChooser extends JFrame implements ActionListener
{
//create a label
private JLabel response;
File file;
//menu tabs
private JMenu fileMenu;
private JMenu editMenu;
private JMenu helpMenu;
String line;
//create a file chooser
private JFileChooser fc;
BufferedReader br;
//create a text area
JTextArea ta = new JTextArea();
//constructors
public ClassChooser
{
//create scroll pane
JScrollPane scrollPane = new JScrollPane(ta);
ta.setText("Enter text to see scroll bars.");
//create a panel
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(scrollPane, BorderLayout.CENTER);
//call functions to create drop down menu's
createFileMenu();
createEditMenu();
createHelpMenu();
//create menu bar and add drop down menu's
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
this.setContentPane(content);
this.setTitle("File Chooser");
this.setVisible(true);
this.setSize(600,250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void createFileMenu()
{
JMenuItem item;
fileMenu = new JMenu("File");
item = new JMenuItem("New");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Open");
item.addActionListener(new OpenListener());
fileMenu.add(item);
item = new JMenuItem("Save");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Rename");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Delete");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Make Directory");
item.addActionListener(this);
fileMenu.add(item);
fileMenu.addSeparator();
item = new JMenuItem("Exit");
item.addActionListener(this);
fileMenu.add(item);
}
public void createEditMenu()
{
JMenuItem item;
editMenu = new JMenu("Edit");
item = new JMenuItem("Cut");
item.addActionListener(this);
editMenu.add(item);
item = new JMenuItem("Copy");
item.addActionListener(this);
editMenu.add(item);
item = new JMenuItem("Paste");
item.addActionListener(this);
editMenu.add(item);
}
public void createHelpMenu()
{
JMenuItem item;
helpMenu = new JMenu("Help");
item = new JMenuItem("Welcome");
item.addActionListener(this);
helpMenu.add(item);
item = new JMenuItem("Help Contents");
item.addActionListener(this);
helpMenu.add(item);
}
private class OpenListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
fc = new JFileChooser();
// directories only to be selected
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setSelectedFile(fc.getCurrentDirectory() );
fc.setDialogTitle("Directory Chooser");
fc.setMultiSelectionEnabled(false);
int retVal = fc.showOpenDialog(ClassChooser.this);
//File file;
if(retVal == fc.APPROVE_OPTION)
{
file = fc.getSelectedFile();
try {
br = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
line = br.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(line != null)
{
ta.append(line + "\n");
try {
line = br.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
public static void main(String[] args)
{
ClassChooser fce = new ClassChooser;
String filename = File.separator + "tmp";
}
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
String menuName;
menuName = e.getActionCommand();
if(menuName.equals("Exit"))
{
System.exit(0);
}
else
{
response.setText("Menu Item '" + menuName + "' is selected.");
}
}
}
Your code actually opens up the file but then you are appending in the text area without clearing the contents of the previously loaded file.
So in your OpenListener
class actionPerformed
method add ta.setText("")
as the first statement and then continue with loading the file content.
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class ClassChooser extends JFrame implements ActionListener {
// create a label
private JLabel response;
File file;
// menu tabs
private JMenu fileMenu;
private JMenu editMenu;
private JMenu helpMenu;
String line;
// create a file chooser
private JFileChooser fc = null;
BufferedReader br;
// create a text area
JTextArea ta = new JTextArea();
private String currentFileBeingEdited = null;
// constructors
public ClassChooser() {
// create scroll pane
JScrollPane scrollPane = new JScrollPane(ta);
ta.setText("Enter text to see scroll bars.");
// create a panel
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(scrollPane, BorderLayout.CENTER);
// call functions to create drop down menu's
createFileMenu();
createEditMenu();
createHelpMenu();
// create menu bar and add drop down menu's
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
this.setContentPane(content);
this.setTitle("File Chooser");
this.setVisible(true);
this.setSize(600, 250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void createFileMenu() {
JMenuItem item;
fileMenu = new JMenu("File");
item = new JMenuItem("New");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Open");
item.addActionListener(new OpenListener());
fileMenu.add(item);
item = new JMenuItem("Save");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Rename");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Delete");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Make Directory");
item.addActionListener(this);
fileMenu.add(item);
fileMenu.addSeparator();
item = new JMenuItem("Exit");
item.addActionListener(this);
fileMenu.add(item);
}
public void createEditMenu() {
JMenuItem item;
editMenu = new JMenu("Edit");
item = new JMenuItem("Cut");
item.addActionListener(this);
editMenu.add(item);
item = new JMenuItem("Copy");
item.addActionListener(this);
editMenu.add(item);
item = new JMenuItem("Paste");
item.addActionListener(this);
editMenu.add(item);
}
public void createHelpMenu() {
JMenuItem item;
helpMenu = new JMenu("Help");
item = new JMenuItem("Welcome");
item.addActionListener(this);
helpMenu.add(item);
item = new JMenuItem("Help Contents");
item.addActionListener(this);
helpMenu.add(item);
}
private class OpenListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//ADDED ONLY THIS LINE
ta.setText("");
fc = new JFileChooser();
// directories only to be selected
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setSelectedFile(fc.getCurrentDirectory());
fc.setDialogTitle("Directory Chooser");
fc.setMultiSelectionEnabled(false);
int retVal = fc.showOpenDialog(ClassChooser.this);
// File file;
if (retVal == fc.APPROVE_OPTION) {
file = fc.getSelectedFile();
currentFileBeingEdited = file.getAbsolutePath();
try {
br = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
line = br.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (line != null) {
ta.append(line + "\n");
try {
line = br.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
ClassChooser fce = new ClassChooser();
String filename = File.separator + "tmp";
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String menuName;
menuName = e.getActionCommand();
if (menuName.equals("Exit")) {
System.exit(0);
} else if("Save".equalsIgnoreCase(menuName)){
PrintWriter pw = null;
try {
pw = new PrintWriter(new File(currentFileBeingEdited));
pw.println(ta.getText());
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} finally {
if(pw != null){
pw.close();
}
}
} else {
response.setText("Menu Item '" + menuName + "' is selected.");
}
}
}