JTextArea to String[]

Does anyone know how I can convert a JTextArea to a String []

[68 byte] By [musiigea] at [2007-11-27 10:15:19]
# 1

I dont think its possible to convert a kettle to coffee, is it? I mean that a JTextArea is quite not like String object!

Jamwaa at 2007-7-28 15:38:47 > top of Java-index,Java Essentials,Java Programming...
# 2

Am thinking about loading the contents of a JTextArea into a String[]

musiigea at 2007-7-28 15:38:47 > top of Java-index,Java Essentials,Java Programming...
# 3

Well with the information, this could be a solution:String[] strings = new String[1];

strings[0] = textArea.getText();

dwga at 2007-7-28 15:38:47 > top of Java-index,Java Essentials,Java Programming...
# 4

Thanks man but I forgot to mention that i want each line in the JTextArea to be placed at a position in the Array

musiigea at 2007-7-28 15:38:47 > top of Java-index,Java Essentials,Java Programming...
# 5

That's what I thoughtString[] lines = textArea.getText().split(System.getProperty("line.separator"));

dwga at 2007-7-28 15:38:47 > top of Java-index,Java Essentials,Java Programming...
# 6

> That's what I thoughtString[] lines =

> textArea.getText().split(System.getProperty("line.sepa

> rator"));

JTextArea uses "\n" independently of the OS, I believe.

And if you want to use the elements in the text area's document

(just for fun), here's a start: put some text in the text area and press the button:

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.text.*;

public class TextAreaExample implements Runnable, ActionListener {

private JTextArea textArea;

public void run() {

textArea = new JTextArea(20, 40);

JButton btn = new JButton("parse");

btn.addActionListener(this);

JFrame f = new JFrame();

Container cp = f.getContentPane();

cp.add(new JScrollPane(textArea), BorderLayout.CENTER);

cp.add(btn, BorderLayout.SOUTH);

f.pack();

f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

f.setLocationRelativeTo(null);

f.setVisible(true);

}

public void actionPerformed(ActionEvent evt) {

Document doc = textArea.getDocument();

Element root = doc.getDefaultRootElement();

int ub = root.getElementCount();

for(int i=0; i < ub; ++i) {

Element line = root.getElement(i);

int lo = line.getStartOffset();

int hi = line.getEndOffset();

System.out.println(lo + " " + hi);

}

}

public static void main(String[] args) {

EventQueue.invokeLater(new TextAreaExample());

}

}

BigDaddyLoveHandlesa at 2007-7-28 15:38:47 > top of Java-index,Java Essentials,Java Programming...