textArea problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saytri
    New Member
    • Dec 2007
    • 35

    textArea problem

    I am displaying the contents of a textfile in a textArea. I want that when i edit this text in the textArea it is automatically saved in the textfile. I have wriitten this code, but the problem is that when i edit the text in the text Area it isn't saving it on the textfile. What do i have wrong? Thanks a lot.

    Code:
    import java.awt.BorderLayout;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    
    import javax.swing.JDialog;
    import javax.swing.JTextArea;
    import java.awt.*;
    import javax.swing.*;
    
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionListener.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import java.util.Calendar;
    import java.awt.image.*;
    import javax.imageio.*;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.IOException;
    import java.io.EOFException;
    import java.io.FileNotFoundException;
    
    
    
    public class Rock_Notes_Editable {
    
    private JTextArea textArea;
    public Rock_Notes_Editable() {
    super();
    
    try{
    JDialog dialog = new JDialog();
    dialog.getContentPane().setLayout(new BorderLayout(10, 10));
    
    
    		
    Test4Rx imagePanel = new Test4Rx("rocks.gif");
            dialog.add(imagePanel, "North");
            dialog.add(new Test4Rx("rocks.gif"));
    //
    String record = null;
    File file = new File("rock.txt");
    FileInputStream fileInput = new FileInputStream(file);
    DataInputStream dis = new DataInputStream(fileInput);
    
    String string = dis.readLine();
    textArea = new JTextArea();
    while(string != null){
    textArea.append(string + "\n");
    string = dis.readLine();
    textArea.setBackground(Color.pink);
    textArea.setFont(new Font("Times New Roman", Font.BOLD, 14));
    textArea.setEditable(true);
    textArea.setForeground(Color.blue);
    BufferedWriter w = new BufferedWriter(new FileWriter("rock.txt", true));
    try {
    
     textArea.write(w);
    
    } finally {
       w.close();
    }
    
    
    }
    dialog.getContentPane().add(BorderLayout.CENTER, textArea);
    dialog.setTitle("Geography Notes");
    dialog.setSize(800, 900);
    
           
    
    dialog.setVisible(true);
    
    
    }catch (Exception e){
    e.printStackTrace();
    }
    
    }
    
    public static void main(String[] args){
    new Rock_Notes_Editable();
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Use FileReader and FileWriter to read/write character files.
    2.) Where is the code that handles the saving of the edited contents part? You'll need to handle at least one event for that.

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      I see you are using JTextComponent' s write method (but in the wrong place). Why not simplify your code and use the corresponding read method too?

      Comment

      • saytri
        New Member
        • Dec 2007
        • 35

        #4
        Ok thanks a lot. :-)

        Comment

        Working...