Retrieving Text from a JTextComponent

This example works for all types of text components.
// Create the text component JTextComponent tc = new JTextArea("Initial Text"); // Get length int docLength = tc.getDocument().getLength(); // Get all text String text = tc.getText(); try { // Get the first 3 characters int offset = 0; int len = 3; text = tc.getText(offset, len); // Get the last 3 characters offset = docLength-3; len = 3; text = tc.getText(offset, len); } catch (BadLocationException e) { }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.