Bejan Alina-Elena
Bejan Alina-Elena

Reputation: 11

how can I get the content of my JTextPane?

Hy everyone! I'm stuck in a little problem.

I need some help or an idea, something...I have some words in a JTextPane and I want to read them one by one.

I know I have to do it with StringTokenizer or with this

Element doc=textPane.getDocument().getDefaultRootElement(),

Thank you for any idea, or help. Any suggestion would be appreciated

Upvotes: 0

Views: 230

Answers (1)

wchargin
wchargin

Reputation: 16037

You can get the text from a JTextPane with getText():

String text = txtpane.getText();

Then, if you want to get each word, you could split around each non-word character using a regex:

String[] words = text.split("\\W"); // "\\W" is \W, which is non-word characters

If you just want to do it based on whitespace, you could use:

String[] words = text.split("\\s"); // "\\s" is \s, which is whitespace

Then, to "read them one by one," iterate over each element in the array:

String text = txtpane.getText();
String[] words  text.split("\\W");
// or: String[] words = txtpane.getText().split("\\W")
for (String word : words) {
    System.out.println(word);
    // do whatever
}

Upvotes: 2

Related Questions