Results 1 to 3 of 3
Thread: lineSeparator in textArea
- 07-03-2012, 06:57 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
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); } }
- 07-03-2012, 08:46 AM #2
Member
- Join Date
- Apr 2012
- Posts
- 6
- Rep Power
- 0
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.
- 07-03-2012, 08:52 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Adding to TextArea
By nawl in forum New To JavaReplies: 10Last Post: 05-21-2010, 07:56 AM -
Output to TextArea
By ljk8950 in forum AWT / SwingReplies: 4Last Post: 08-03-2008, 09:59 AM -
TextArea with Unicode
By Java Tip in forum javax.swingReplies: 0Last Post: 04-16-2008, 10:53 PM -
TextArea Bug?
By Soda in forum New To JavaReplies: 2Last Post: 12-07-2007, 12:37 PM -
textarea
By ubuntu in forum AWT / SwingReplies: 4Last Post: 05-12-2007, 09:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks