Question

i am trying to make myself a word processor. I have only become familiar with the concept of the JFileChooser and have implemented it into my code. My hope is to be able to use a JFileChooser to open a text file and append the contents of the text file to the JTextArea. To try my best at seperating my functionality i implemented the MVC (model view controller) so i have 3 classes. the view is called WordFrame.java, the model is called DataStuff.java and the controller is called ProcessEvents.java. My issue is rather simple, i can print the contents of a text file to the console, but attempts to append the said contents to the TextArea is only resulting in white space, the image below shows the position on the textArea, signifying its character spacing, this is after the file is loaded. As stated the console prints it out perfectly.

enter image description here

also, here is code for the model and controller

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Scanner;


public class ProcessEvents {

    private WordFrame frame = new WordFrame();
    private DataStuff data = new DataStuff();
    private DialogBoxes dialogs = new DialogBoxes();
    private boolean fileSaved;
    String fileName = "";
    int fontSize = 0;
    public ProcessEvents(WordFrame frame, DataStuff data){
        this.frame = frame;
        this.data = data;
        this.frame.addListener(new wordProcessListener());
    }
    class wordProcessListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource().equals(frame.openMenuItem)){
                frame.fileChooser.showOpenDialog(frame);
                File f = frame.fileChooser.getSelectedFile();

                System.out.println("Command Executed: open");

                data.loadFile(f.getAbsoluteFile());

                if(data.showText() != null){
                    String texts = data.showText().toString();

                    System.out.println(data.showText());

                    frame.textArea.append(data.showText().toString());
                }
            }
            if(e.getSource().equals(frame.FontMenuItem)){
                System.out.println("font");
            }
            if(e.getSource().equals(frame.exitMenuItem)){
                dialogs.getConfirmDialog("exitWithoutSave");
            }

            if(e.getSource().equals(frame.saveMenuItem)){
                dialogs.getSaveFileDialog();
                data.saveFile("Bingo");
                fileSaved = true;
            }
            if(e.getSource().equals(dialogs.close)){
                if(!fileSaved){

                }
            }
        }

    }
}

here is the model

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataStuff {
    private String fileName = "";
    private File file;

    String text;
    String name;
    private File saveFile;
    int counter = 0;
    FileInputStream fis = null;
    FileOutputStream fout = null;
    StringBuilder sb;
    char[] charArray;
    int count = 0;
    public void loadFile(File fileName){
        this.file = fileName;
        try{
            fis = new FileInputStream(file);
            charArray = new char[fis.read()];
            while ((counter = fis.read()) != -1) {

                System.out.print((char) counter);
                sb = new StringBuilder();   
                sb.append(charArray);

            }

        }
        catch(IOException ex){
            ex.printStackTrace();
        }
        finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    public StringBuilder showText(){

        return sb;

    }
    public void saveFile(String name) {
        this.name = name;
        try{
        fout = new FileOutputStream(saveFile+"/"+name+".txt");
        }
        catch(IOException e){
            System.out.println("File failed to save or something went horribly wrong");
        }
    }
}

and as always, assistance is always appreciated.

Edit: So i actually fixed this solution with the assistance of the guys below, i decided i didn't want to replace the code above just in case someone else stumbles on the same issue and can see the differences, here is the view class again with the adjustments.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataStuff {
    private String fileName = "";
    private File file;

    String text;
    String name;
    private File saveFile;
    int counter = 0;
    FileInputStream fis = null;
    FileOutputStream fout = null;
    StringBuilder sb = new StringBuilder(4096);
    //char[] charArray = new char[4096];
    int count = 0;
    public void loadFile(File fileName){
        this.file = fileName;
        try{
            fis = new FileInputStream(file);
        //  charArray = Character.toChars(fis.read());
            while ((counter = fis.read()) != -1) {

                System.out.print((char) counter);

                sb.append((char) counter);

            }

        }
        catch(IOException ex){
            ex.printStackTrace();
        }
        finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    public StringBuilder showText(){

        return sb;

    }
    public void saveFile(String name) {
        this.name = name;
        try{
        fout = new FileOutputStream(saveFile+"/"+name+".txt");
        }
        catch(IOException e){
            System.out.println("File failed to save or something went horribly wrong");
        }
    }
}
Was it helpful?

Solution

In a first review it looks like you are creating a new string builder on each loop iteration instead of reusing it. You should create the stringbuilder before starting the while loop and on each iteration simply append read characters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top