Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java: Complete the code by adding to the // load file & // save file import java

ID: 3686301 • Letter: J

Question

Java: Complete the code by adding to the // load file & // save file

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class TextFileFun extends JFrame implements ActionListener

{

   private JButton B1, B2;

   private JTextArea text;

private Container pane;

private String filename;

   public TextFileFun()

   {

       //use JFrame's constructor to title window

       super("Text Area and File Fun");

  

filename = "textFunFile.txt"; // filename for text area data

       setBounds(400,100,400,500); //position and size of window

       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  

pane = getContentPane(); // reference to the contentPane of window

       //create JButtons and JLabel

       B1 = new JButton("Load");

       B2 = new JButton("Save");

text = new JTextArea(10,40);

       

       //add the controls to the contentPane

       pane.add(B1, BorderLayout.LINE_START);

       pane.add(text, BorderLayout.CENTER);

       pane.add(B2, BorderLayout.LINE_END);

       //connect the buttons to the listener

       //listener is in this object

       B1.addActionListener( this );

       B2.addActionListener( this );

  

  

addWindowFocusListener(new WindowAdapter()

{

public void windowGainedFocus(WindowEvent e)

{

text.requestFocusInWindow();

}

});

  

pack();

       setVisible(true);

   }// end of constructor

   public void actionPerformed(ActionEvent ae)

   {

       if ( ae.getSource()==B1 )

{

// load file

}

else if ( ae.getSource()==B2 )

{

// save file

}

   }

   public static void main(String args[])

   {

       TextFileFun f = new TextFileFun();

   }

}

Explanation / Answer

Hello there ,

Please find below code and o/p : I am unable to upload image of O/P.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileReader;
import java.io.FileWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class TextFileFun extends JFrame implements ActionListener {
   /**
   *
   */
   private static final long serialVersionUID = 1L;
   private JButton B1, B2;
   private JTextArea text;
   private Container pane;
   private String filename;

   public TextFileFun() {
       // use JFrame's constructor to title window
       super("Text Area and File Fun");

       filename = "textFunFile.txt"; // filename for text area data

       setBounds(400, 100, 400, 500); // position and size of window
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       pane = getContentPane(); // reference to the contentPane of window

       // create JButtons and JLabel
       B1 = new JButton("Load");
       B2 = new JButton("Save");
       text = new JTextArea(10, 40);
       text.setLineWrap(true);

       // add the controls to the contentPane
       pane.add(B1, BorderLayout.LINE_START);
       pane.add(text, BorderLayout.CENTER);
       pane.add(B2, BorderLayout.LINE_END);

       // connect the buttons to the listener
       // listener is in this object
       B1.addActionListener(this);
       B2.addActionListener(this);

       addWindowFocusListener(new WindowAdapter() {
           public void windowGainedFocus(WindowEvent e) {
               text.requestFocusInWindow();
           }
       });

       pack();
       setVisible(true);
   }// end of constructor

   public void actionPerformed(ActionEvent ae) {
       if (ae.getSource() == B1) {
           try {
               load(text, filename);
           } catch (Exception e) {
               e.printStackTrace();
           }
       } else if (ae.getSource() == B2) {
           try {
               save(text, filename);
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
   }

   /**
   * It will save file.
   * @param text
   * @param inputFile
   * @throws Exception
   */
   public static void save(JTextArea text, String inputFile) throws Exception {

       FileWriter writer = null;

       writer = new FileWriter(inputFile);

       text.write(writer);

       writer.close();
   }

   /**
   * It will load file in TextArea.
   * @param text
   * @param inputFile
   * @throws Exception
   */
   public static void load(JTextArea text, String inputFile) throws Exception {

       FileReader inputReader = null;

       inputReader = new FileReader(inputFile);

       text.read(inputReader, inputFile);

       inputReader.close();

   }

   public static void main(String args[]) {
       TextFileFun f = new TextFileFun();
   }
}

Let me know if you have any queries.

Thanks.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote