Results 1 to 3 of 3
  1. #1
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default lineSeparator in textArea

    The following code work with two exceptions:

    1. The lineSeparator in the textArea doesn´t work. I have tried "textArea.append(read + lineTerminator);" but then nothing is written in the textArea.

    2. I don´t know how I can add several inputs without restart the javafile. I think I have to put another ActionListener somewhere, but where?

    Java Code:
    import java.awt.*;   
    import java.awt.event.*;  
    import javax.swing.*;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
     public class ReadWriteTextFile extends JFrame  {   
    	 // Data members
    	 private static JTextField inputLine;   
         private static JButton button;  
         private static JTextArea textArea;
         private BufferedReader in;
         private BufferedWriter out; 
         private static final String EMPTY_STRING = "";
         private static String lineTerminator = System.getProperty("line.separator");
         private int numberOfLines=50;
         private String read;
         
         // Constructor
         public ReadWriteTextFile() throws IOException  
         {  
             setTitle("Skriv text till fil");  
             setSize(300,400);   
             setLayout(new FlowLayout());   
             inputLine = new JTextField();  
             inputLine.setColumns(25);
             button = new JButton("Klicka här"); 
             textArea = new JTextArea(20,50);
             add(inputLine);  
             add(button); 
             add(textArea);
        
             // stänger systemet när fönstret stängs
             addWindowListener(new WindowAdapter()   
             {   
                public void windowClosing(WindowEvent e)   
                 {   
                    System.exit(0);   
                 }   
             });  
    
             // reagerar på knapptrycksningsevent
             button.addActionListener(new ActionListener()   
             {   
                 public void actionPerformed(ActionEvent event)  
                {     
                	 // kod för att skriva text till filen
                	 try {  
                		out = new BufferedWriter(new FileWriter("C:/Users/Gunilla/Documents/My Dropbox/Javakurs2/Bokexempel/src/test.txt",true)); 
                     	out.write(inputLine.getText());  
                 		out.newLine();
                 		out.close();  
                     	}
                   	catch(IOException e){  
                   			System.out.println("There was a problem:" + e);  
                   	} 
                	 
                	 // kod för att läsa text från filen
                	 try {  
                		   in = new BufferedReader(new FileReader("C:/Users/Gunilla/Documents/My Dropbox/Javakurs2/Bokexempel/src/test.txt"));  
                                while(numberOfLines > 0){  
                                 	read = in.readLine();  
                                	System.out.println("file output: " + read); 
                                	textArea.append(read);
                                	numberOfLines--; 
                                	inputLine.setText("");
                            }  
                             in.close();  
                	   		}catch(IOException e){  
                	   			System.out.println("There was a problem:" + e);  
                	   		} 
                	  }
             });  
       }  
    
         // Main-metod
        public static void main(String[] args) throws IOException   
         {   
        	ReadWriteTextFile guiInterface = new ReadWriteTextFile();   
            guiInterface.setVisible(true);  
        }  
     }

  2. #2
    jitu.ideas2it is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: lineSeparator in textArea

    String textarea = textComments.getText();
    int index = textarea.firstIndexOf( CHR(10) );


    Then I'd split the text and create a new string with the \n character.

  3. #3
    jitu.ideas2it is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: lineSeparator in textArea

    textArea.append(read); Instead Of That Use String Buffer And Append The Text Then String Buffer U Can Set For The TextArea Try Like This

Similar Threads

  1. Adding to TextArea
    By nawl in forum New To Java
    Replies: 10
    Last Post: 05-21-2010, 07:56 AM
  2. Output to TextArea
    By ljk8950 in forum AWT / Swing
    Replies: 4
    Last Post: 08-03-2008, 09:59 AM
  3. TextArea with Unicode
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-16-2008, 10:53 PM
  4. TextArea Bug?
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 12:37 PM
  5. textarea
    By ubuntu in forum AWT / Swing
    Replies: 4
    Last Post: 05-12-2007, 09:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •