Java Code Examples for javax.swing.text.html.HTMLDocument#getElement()

The following examples show how to use javax.swing.text.html.HTMLDocument#getElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private static void scrollToId(JEditorPane editor, String id) {
  Document d = editor.getDocument();
  if (d instanceof HTMLDocument) {
    HTMLDocument doc = (HTMLDocument) d;
    Element element = doc.getElement(id);
    try {
      int pos = element.getStartOffset();
      // Java 9: Rectangle r = editor.modelToView2D(pos).getBounds();
      Rectangle r = editor.modelToView(pos);
      if (r != null) {
        Rectangle vis = editor.getVisibleRect();
        r.height = vis.height;
        editor.scrollRectToVisible(r);
        editor.setCaretPosition(pos);
      }
    } catch (BadLocationException ex) {
      UIManager.getLookAndFeel().provideErrorFeedback(editor);
    }
  }
}
 
Example 2
Source File: FSwingHtml.java    From pra with MIT License 5 votes vote down vote up
public static Element findElement(HTMLDocument doc
		, Attribute ab, String value){
	
	return doc.getElement(doc.getDefaultRootElement(), ab, value);
	
   /*ElementIterator it = new ElementIterator(doc);
   Element e;
   while ((e = it.next()) != null) 
     if (hasAttribute(e,ab,value))return e;
	return null;*/	
}
 
Example 3
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void actionPerformed(ActionEvent e) {
  textArea.append(String.format("----%n%s%n", getValue(Action.NAME)));
  String id = field.getText().trim();
  HTMLDocument doc = (HTMLDocument) editorPane.getDocument();
  Element element = doc.getElement(id);
  if (Objects.nonNull(element)) {
    textArea.append(String.format("found: %s%n", element));
    editorPane.requestFocusInWindow();
    editorPane.select(element.getStartOffset(), element.getEndOffset());
  }
}