-1

I have a JTextArea

matrix

When I write this data to text file, it prints all on the same line:

gribi

How to make the output of text file to be the same like in text area, on 2 separate lines?

The code of text area, and writing data to text.

JTextArea matrixArea = new JTextArea();
matrixArea.setBounds(139, 63, 337, 111);
dataPanel.add(matrixArea);

JButton sendData = new JButton("Send Data");
sendData.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        String nrPatterns = patternText.getText().toString();
        String inputTex = inputText.getText().toString();
        String otputTex = outputText.getText().toString();
        String matrixW  = matrixArea.getText().toString();

        try {
            FileWriter writer = new FileWriter("test.txt", true);
            writer.write(nrPatterns);
            writer.write(" ");
            writer.write(inputTex);
            writer.write(" ");
            writer.write(otputTex);
            writer.write(" ");
            writer.write(System.getProperty("line.separator"));
            writer.write(matrixW);
            writer.close();
            JOptionPane.showMessageDialog(window, "Success");
        }catch(Exception e){
            JOptionPane.showMessageDialog(window, "Error", "Error",  JOptionPane.ERROR_MESSAGE);
        }
    }
});
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • is split("\n") not working? – johnII Jan 17 '18 at 00:28
  • Ah, note pad, you pain in the ... code. Note pad requires both a `\n` (new line) and `\r` (return) before it will print the lines. You could try loading the file into a different editor or use `System.lineSeparator` and replace all the `\n` characters in the `String`s with it ... as a few ideas of the top the head - [For more info](https://stackoverflow.com/questions/5557978/notepad-not-recognizing-new-line-characters-in-java-code-using-printstream-to-ou) – MadProgrammer Jan 17 '18 at 00:29
  • tried, didn't help :( – Tutunaru Dan Marin Jan 17 '18 at 00:34
  • 1) See [`JTextComponent.write(java.io.Writer)`](https://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html#write-java.io.Writer-) 2) `matrixArea.setBounds(139, 63, 337, 111);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Jan 17 '18 at 00:55
  • .. Use rows/cols to suggest a size for this text area, and layouts constraints for the positioning. 3) *"tried, didn't help"* Tip: Add @MadProgrammer (or whoever, the `@` is important) to *notify* the person of a new comment. 4) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jan 17 '18 at 00:56

1 Answers1

0

Use this after your last .write:

writer.newLine();