Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6881763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:10:13+00:00 2026-05-27T05:10:13+00:00

I am trying to open a file in a JTextArea and then write and

  • 0

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:10:13+00:00Added an answer on May 27, 2026 at 5:10 am

    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.");
        }
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to open a file for read/write. I've been developing on Ubuntu, and
I'm trying to open a file and create a list with each line read
I am trying to open html file http://www.geocodezip.com/v3_MW_example_map3.html from my computer. I can open
I'm trying to open a write-protected ms excel 2007 file using win32com in python
I am trying to open a file which a user can set. In other
I'm trying to open a file, and read from it.. but I'm having some
I'm trying open a txt file and read it, but I'm getting a type
I am trying to open file in function that loads arguments and then save
Trying to open an image for editing in C# I can open the file
I'm trying to open a file from Java in Ubuntu using FileInputStream.Now my problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.