Question

I use a JTextArea in which I use setText method to have some text while opening GUI.

Once text area is opened with the text I set, I typed some text, my intention is to get whatever text user types.

dataField = new JTextArea();
dataField.setText("sample#");
..
...

If I type "hello world"

sample#hello world

in text area and press enter I need to get only hello world in a string and not sample#hello world. I have tried with key listeners and appended the input characters to a string builder but backspace also creates a unreadable character an appends to it.

Simply put, I need to get user typed text from text area.

Was it helpful?

Solution

sample# is the last line in text area , so what ever i type after the last token i need to get that text.

Read the JTextArea API:

  1. getLineCount() so you know the number of lines
  2. getLineStartOffset() and getLineEndOffset() to know the offsets of the text you need to get from text area. Add 7 to the start since you don't want to get the "sampler#" text.
  3. getText() to get the text using the offsets from above.

OTHER TIPS

String a="sample#"+datafield.getText();

Try this

String old = "sample#";
String s = dataField.getText().subString(old.length());

Good luck

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top