pengpeng_yu pengpeng_yu - 6 months ago 12
Java Question

Copying text between files and JTextArea

package javaFinal;
import java.io.*;
import java.util.EventListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.JTextComponent;


I want to make it to achieve the function that add the content from text1.txt to JTextArea and to copy it to another JTextArea. Finally when I click JButton 'jbt2', all words copy to the text2.txt.Here are my code:

public class Copy_Frame extends JFrame
{

static JButton jbt_input;
static JButton jbt_output;
static JButton jbt_copy;
static JTextArea jtr1;
static JTextArea jtr2;

public static void main(String [] args){
JFrame jfrm = new JFrame("output");
JTextArea jtr1 = new JTextArea("leadin:",10,40);
JTextArea jtr2 = new JTextArea("output:",10,40);
JScrollPane jsp1 = new JScrollPane(jtr1);
JScrollPane jsp2 = new JScrollPane(jtr2);
FlowLayout flow = new FlowLayout();
JButton jbt_input = new JButton("leadin");
JButton jbt_output = new JButton("output");
JButton jbt_copy = new JButton("copy");
jbt_input.setBounds(45,45,100,40);
jbt_output.setBounds(45,45,100,160);
jbt_copy.setBounds(45,45,100,100);
jfrm.add(jsp1);
jfrm.add(jsp2);
jfrm.add(jbt_copy);
jfrm.add(jbt_output);
jfrm.add(jbt_input);
jfrm.setLayout(flow);
jfrm.setSize(1000,700);
jfrm.setVisible(true);
jbt_input.addActionListener(new Mylistener());
jbt_output.addActionListener(new Mylistener());
jbt_copy.addActionListener(new Mylistener());

}
static public class Mylistener implements ActionListener{
Mylistener mylistener = new Mylistener();
public void actionPerformed(ActionEvent e) {
JButton jbt = (JButton)e.getSource();
String s;
char []ch = new char[100] ;
if (jbt == (JButton) jbt_input)
try{
File f = new File("d:\\text1.txt");
FileReader fr = new FileReader(f);
fr.read(ch);
jtr1.setText(ch.toString());
fr.close();
} catch (IOException e1){}

else if (jbt == jbt_output)
try{
File f = new File("d:\\text2.txt");
FileWriter fw = new FileWriter(f);
s = jtr2.getText();
fw.write(s);
fw.close();
} catch (IOException e1){}
}
}
}


But the result is only the JFrame exit. Nothing happened when I click the two JButton.why??

Answer

There are several problems in your code:

  1. Instance level variables such as jbt_input, jbt_output had been
    shadowed by method level variables.
  2. There is no need for Mylistener mylistener = new Mylistener(); inside the MyListener class.
  3. Loading text from file into text area was not correct.
  4. There was no code for copying text from one text area to another etc.

Please look at the following solution:

package javaFinal;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

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

public class Copy_Frame extends JFrame {
    private static final long serialVersionUID = 1L;
    static JButton jbt_input; // loads content of a file: "d:\\text1.txt"
    static JButton jbt_output; // writes content of text area jtr2 to file: "d:\\text2.txt"
    static JButton jbt_copy; // copies content of text area jtr1 to text area jtr2
    static JTextArea jtr1; // text area of left side
    static JTextArea jtr2;// text area of right side

    public static void main(String[] args) {
        JFrame jfrm = new JFrame("output");
        jtr1 = new JTextArea("leadin:", 10, 40);
        jtr2 = new JTextArea("output:", 10, 40);
        JScrollPane jsp1 = new JScrollPane(jtr1);
        JScrollPane jsp2 = new JScrollPane(jtr2);
        FlowLayout flow = new FlowLayout();
        jbt_input = new JButton("leadin");
        jbt_output = new JButton("output");
        jbt_copy = new JButton("copy");
        jbt_input.setBounds(45, 45, 100, 40);
        jbt_output.setBounds(45, 45, 100, 160);
        jbt_copy.setBounds(45, 45, 100, 100);
        jfrm.add(jsp1);
        jfrm.add(jsp2);
        jfrm.add(jbt_copy);
        jfrm.add(jbt_output);
        jfrm.add(jbt_input);
        jfrm.setLayout(flow);
        jfrm.setSize(1000, 700);
        jfrm.setVisible(true);
        jbt_input.addActionListener(new Mylistener());
        jbt_output.addActionListener(new Mylistener());
        jbt_copy.addActionListener(new Mylistener());

    }

    private static class Mylistener implements ActionListener {
        private static final String NEW_LINE = System.getProperty("line.separator");

        public void actionPerformed(ActionEvent e) {
            System.out.println(NEW_LINE);
            JButton jbt = (JButton) e.getSource();
            if (jbt == (JButton) jbt_input){
                // loading content of file into left side text area
                try {
                    File f = new File("d:\\text1.txt");
                    BufferedReader br = new BufferedReader(new FileReader(f));
                    String fileContent = "";
                    String line = null;
                    while ((line = br.readLine()) != null) {
                        fileContent += line + NEW_LINE;
                    }
                    br.close();
                    jtr1.setText(fileContent);

                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }else if (jbt == jbt_copy) {
                // loading content of left side text area into that of right side
                jtr2.setText(jtr1.getText());
            } else if (jbt == jbt_output) {
                // writing content of right side text area into file
                try {
                    File f = new File("d:\\text2.txt");
                    PrintWriter pw = new PrintWriter(new FileWriter(f));
                    String str = jtr2.getText();
                    pw.println(str);
                    pw.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}

and tell me whether you wanted to achieve this or not?

Comments