JTextArea to String[]
Does anyone know how I can convert a JTextArea to a String []
Does anyone know how I can convert a JTextArea to a String []
I dont think its possible to convert a kettle to coffee, is it? I mean that a JTextArea is quite not like String object!
Well with the information, this could be a solution:String[] strings = new String[1];
strings[0] = textArea.getText();
Thanks man but I forgot to mention that i want each line in the JTextArea to be placed at a position in the Array
That's what I thoughtString[] lines = textArea.getText().split(System.getProperty("line.separator"));
> 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());
}
}