JTextPane textPane = new JTextPane(); Element element = textPane.getDocument().getDefaultRootElement(); int startOffset = element.getStartOffset(); // Returns the starting offset of the root element
JTextArea textArea = new JTextArea(); Document doc = textArea.getDocument(); Element root = doc.getDefaultRootElement(); Element paragraph = root.getElement(0); // first element is the paragraph int startOffset = paragraph.getStartOffset();In both examples, we are getting the starting offset of the root element and the first paragraph element respectively. Package Library: javax.swing.text.