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

1 Overview In this lab, you will complete the implementation of a le editor prog

ID: 3775017 • Letter: 1

Question

1 Overview

In this lab, you will complete the implementation of a le editor program that

uses JTextArea to display the content of a text file. You will embed this text

area using a JScrollPane object, which enables scrolling for text larger than

the viewing area. You will register a listener to a load button and a save button

so that if load button is clicked, it loads a le using JFileChooser and if save

button is clicked, it saves the text in the text area to the loaded file. The actual

save and load operations are already implemented in the FileEditor class.

2 Screenshot

Please see the screenshot on how a working version may look like.

package lab11;

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;

import javax.swing.JButton;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

public class Lab11 extends JFrame {

   public Lab11(){

       setTitle("CS251-Editor");

       setBounds(200, 200, 600, 600);

       setDefaultCloseOperation(EXIT_ON_CLOSE);

   }

   private static final long serialVersionUID = 1L;

   // Main program for the application

   public static void main(String[] args) {

       JFrame frame = new Lab11();

       // set border layout

       // TODO

       // panel for the two buttons

       // TODO

       // button for load and save file

       // TODO

       // add buttons to panel

       // TODO

       // disable save button since it makes no sense to save before there is file

       // TODO

       // create a text area

       // TODO

       // put it into a scroll pane so that the text can be larger than viewable area

       // TODO

       // add scrollable text to the center of the frame

       // add button panel to the top

       // TODO

       // create a file editor instance

       FileEditor editor = new FileEditor(text);

       // create a listener that loads file if load button is clicked

       // if file is loaded successfully, it enables the save button

       // the listener saves file if save button is clicked

       ActionListener listener = new ActionListener() {

           @Override

           public void actionPerformed(ActionEvent event) {

               // TODO

           }

       };

       // register listener to the buttons

       // TODO

       frame.setVisible(true);

   }

}

class FileEditor {

   private JTextArea text;

   // file loaded and to be saved

   private File file = null;

   // save the text area

   FileEditor(JTextArea text) {

       this.text = text;

   }

   // returns true if and only if a file is successfully loaded

   boolean loadFile() {

       File selectedFile = null;

       // file chooser allows user to select a file from the default directory

       JFileChooser dialog = new JFileChooser();

       // use a new frame to display open dialog

       int result = dialog.showOpenDialog(new JFrame());

       // if user approved chosen file

       if (result == JFileChooser.APPROVE_OPTION) {

           // get the selected file

           selectedFile = dialog.getSelectedFile();

           // create a scanner for the file

           Scanner in = null;

           try {

               in = new Scanner(selectedFile);

           }

           catch (FileNotFoundException e) {

               e.printStackTrace();

           }

           // use the scanner to read the file line by line

           String content = "";

           while (in.hasNextLine()) {

               content = content + in.nextLine() + " ";

           }

           // close scanner

           if(in != null) { in.close(); }

           // set the text area with the read file

           this.text.setText(content);

       }

       // save selected file and return true if and only if the selected file is not null

       boolean ret = false;

       if(selectedFile != null) {

           this.file = selectedFile;

           ret = true;

       }

       return ret;

   }

  

   // if the file is not null, use PrintWriter to write the content of the text area into the file

   void saveFile() {

       if(this.file != null) {

           PrintWriter out = null;

           try {

               out = new PrintWriter(this.file);

               out.println(this.text.getText());

           }

           catch (FileNotFoundException e) {

               e.printStackTrace();

           }

           finally {

               if(out != null) { out.close(); }

           }

       }

   }

}

CS251-Editor Save File gnore thumbnails created by windows Thumbs.db #Ignore files build by Visual Studio obj exe pdb user aps pch VS psCC i.C ncb Suo tlb tlh bak cache *.ilk og *.dll *.lib Sbr Load Fil

Explanation / Answer

import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JButton; public class Demo { private JFrame frame; private JTextField txtPath; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Demo window = new Demo(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public Demo() { initialize(); } private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); txtPath = new JTextField(); txtPath.setBounds(10, 10, 414, 21); frame.getContentPane().add(txtPath); txtPath.setColumns(10); JButton btnBrowse = new JButton("Browse"); btnBrowse.setBounds(10, 41, 87, 23); frame.getContentPane().add(btnBrowse); btnBrowse.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); // For Directory fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // For File //fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setAcceptAllFileFilterUsed(false); int rVal = fileChooser.showOpenDialog(null); if (rVal == JFileChooser.APPROVE_OPTION) { txtPath.setText(fileChooser.getSelectedFile().toString()); } } }); } }