简体   繁体   中英

Save From buffered reader to JTextArea

I want to make the result of my buffered reader to appear in a text area, but It doesn't work for me.

I want the text area to get the result exactly as the system out print do, it is for more than one line, I tried to set the text area with string s but didn't work, just give me the result of one line.

Here is my Code:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JTextArea;

import java.awt.ScrollPane;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Window extends JFrame {

/**
 * Launch the application.
 * @throws FileNotFoundException 
 */
public static void main(String[] args) {

    Window frame = new Window();
    frame.setTitle("SWMA Extractor");
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setBounds(50, 50, 665, 550);
    //frame.setLocation(500, 300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.getContentPane().setLayout(null);

}

/**
 * Create the frame.
 */
public Window() {
    setResizable(false);

    JPanel contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel path = new JLabel("File Location");
    path.setHorizontalAlignment(SwingConstants.CENTER);
    path.setBounds(20, 11, 74, 23);
    contentPane.add(path);

    final JTextField location = new JTextField();
    location.setBounds(104, 12, 306, 20);
    contentPane.add(location);
    location.setColumns(10);

    final JTextArea textArea = new JTextArea();

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setBounds(20, 80, 605, 430);
    contentPane.add(scrollPane);
    scrollPane.add(textArea);


    JButton btn = new JButton("Get Info.");
    btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            chooser.showOpenDialog(null);
            File f = chooser.getSelectedFile();
            File output = null;
            try {
                FileReader fr = new FileReader(f);
                BufferedReader br = new BufferedReader(fr);
                String s;
                int lineNumber = 1;
                while((s = br.readLine()) != null) {
                    if (s.contains("System")) {
                        System.out.println(s);
                        String nextLine = br.readLine();
                        System.out.println(nextLine);
                        String nextLine1 = br.readLine();
                        System.out.println(nextLine1);
                        String nextLine2 = br.readLine();
                        System.out.println(nextLine2);
                        String nextLine3 = br.readLine();
                        System.out.println(nextLine3);
                        System.out.println();

                    }
               }
                lineNumber++; 

            } catch (IOException e2) {
                e2.printStackTrace();
            }
        } 
    });
    btn.setBounds(433, 11, 192, 23);
    contentPane.add(btn);

    JButton clr = new JButton("Clear");
    clr.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String getLocation = location.getText();
            String s;
            try {
                textArea.setText("");
                location.setText("");
            } catch (Exception e1) {

            }
        }
    });
    clr.setBounds(20, 45, 605, 23);
    contentPane.add(clr);
}

}

I see you say you tried setting the text, but the setText method actually replaces the whole current text with the new one:

JTextComponent @1669:
((AbstractDocument)doc).replace(0, doc.getLength(), t,null);

You should use insert or append methods:

Replace

System.out.println(s); 

with

textArea.append(s);

Also, check the following question for a better way of doing this:

Opening, Editing and Saving text in JTextArea to .txt file

private void fileRead(){
    try{    
        FileReader read = new FileReader("filepath");
        Scanner scan = new Scanner(read);
            while(scan.hasNextLine()){
            String temp = scan.nextLine() + System.lineSeparator();
            storeAllString = storeAllString + temp;
        }
    }
    catch (Exception exception) {
        exception.printStackTrace();
    }
}      

@Hovercraft's suggestion is very good. If you don't want to process the file in any way, you could directly read it into the JTextArea:

try {
    FileReader fr = new FileReader(f);
    BufferedReader br = new BufferedReader(fr);
    textArea.read(br, "Stream description");
} catch (IOException e2) {
    e2.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM