Home > Blockchain >  Swing Java txt file writing to JTextArea
Swing Java txt file writing to JTextArea

Time:04-14

Is there a way to make the null go away at the end of the file? I decided to append the first line before the while loop so it wouldn't be affected by the \n but in doing so the null appears... (without the first append it just skips the first line and starts printing on the second line)

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;

public class notepadMinus implements ActionListener{
    //Properties
    JFrame theFrame;
    JPanel thePanel;
    JMenuBar theBar;
    JMenu theMenu;
    JMenuItem openItem;
    JMenuItem saveItem;
    JTextArea theArea;
    JScrollPane theScroll;
    JFileChooser openChoose;
    String strLine;
    //Methods
    public void actionPerformed(ActionEvent evt){
        if(evt.getSource() == openItem){
            System.out.println("Open File");
            int intResult = openChoose.showOpenDialog(openItem);
            if(intResult == JFileChooser.APPROVE_OPTION){
                System.out.println("File selected");
                File selectedFile = openChoose.getSelectedFile();
                theArea.selectAll();
                theArea.replaceSelection("");
                try{
                    BufferedReader txtfile = new BufferedReader(new FileReader(openChoose.getSelectedFile()));
                    strLine = txtfile.readLine();
                    theArea.append(strLine);
                    while(strLine != null){
                        strLine = txtfile.readLine();
                        theArea.append( "\n"    strLine);   
                    }
                    txtfile.close();
                }catch(FileNotFoundException e){
                    System.out.println("File Not Found");
                }catch(IOException e){
                    System.out.println("Reading Error");
                }   
                //read lines
                //put those lines in theArea - append
                //System.out.println(openChoose.getSelectedFile());
            }else{
                System.out.println("Cancelled?");
            }
        }
    }
    //Constructor
    notepadMinus(){
        theFrame = new JFrame("Notepad Minus");
        theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        thePanel = new JPanel();
        thePanel.setLayout(null);
        thePanel.setPreferredSize(new Dimension(800, 600));
        
        theBar = new JMenuBar();
        //theBar.setSize(600,50);
        //theBar.setLocation(100, 50);
        //thePanel.add(theBar);
        theFrame.setJMenuBar(theBar);
        
        theMenu = new JMenu("File");
        theBar.add(theMenu);
        
        openItem = new JMenuItem("Open");
        openItem.addActionListener(this);
        theMenu.add(openItem);
        
        saveItem = new JMenuItem("Save");
        saveItem.addActionListener(this);
        theMenu.add(saveItem);
        
        theArea = new JTextArea();
        theScroll = new JScrollPane(theArea);
        theScroll.setPreferredSize(new Dimension(800, 600));
        
        //theFrame.setContentPane(thePanel);
        theFrame.setContentPane(theScroll);
        theFrame.pack();
        theFrame.setVisible(true);
        
        openChoose = new JFileChooser();
        
    }
    //Main Method
    public static void main(String[] args){
        new notepadMinus();
    }
}

What it looks like in the JTextArea

hello world
hello world
hello world
hello world
null

CodePudding user response:

The solution

Change

theArea.append(strLine);
while(strLine != null){
    strLine = txtfile.readLine();
    theArea.append( "\n"    strLine);   
}

to

while(strLine != null){
    theArea.append( "\n"    strLine);   
    strLine = txtfile.readLine();
}

The mistake

By reading just before printing, you are skipping the null check.
You are already reading a line from txtfile before the loop. You should print it first before you read another.
There is another consequence of this mistake - the first line is skipped. If your file had

a
b
c
d

as its contents, the output would be

b
c
d
null

CodePudding user response:

Fixed! the \n should be after strLine:

while(strLine != null){
    theArea.append(strLine   "\n");   
    strLine = txtfile.readLine();
}
  • Related