0

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3164187
  • 1,382
  • 3
  • 19
  • 50
  • The code you have posted is **NOT** enough to help! – Keerthivasan Apr 04 '14 at 09:27
  • 1
    You may be looking for _Text Prompt_, cited [here](http://stackoverflow.com/a/5045770/230513). – trashgod Apr 04 '14 at 09:31
  • Is `sample#` always the same? Just take the substring starting after `sample#` of datafield.getText() – leigero Apr 04 '14 at 09:31
  • @leigero No its not always the same – user3164187 Apr 04 '14 at 10:23
  • I'm not sure I understand the requirement either. Since you are using a JTextArea it implies that you can have multiple lines. Does this mean that the start of each line has a token? Do you only want the text from the last token or all tokens? – camickr Apr 04 '14 at 16:39
  • @camickr The text area contains multiple lines but i am expecting the text from last token.. sample# is the last line in text area , so what ever i type after the last token i need to get that text. – user3164187 Apr 07 '14 at 04:21

3 Answers3

1

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.
camickr
  • 321,443
  • 19
  • 166
  • 288
0

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

M.Raheel
  • 165
  • 7
0

Try this

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

Good luck

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45