title: How to remove the last line/component/icon/widget/etc. from JTextPane
body: <p>I have a massive Java project which utilizes a console like UI. For the console, I use a JTextPane. Recently I have need for methods to remove the first and last line respectively. The method I have for removing the first line is quite trivial and is as follows</p>
  <pre class="lang-java prettyprint-override"><code>public void removeFirstLine() {
          try {
              Element root = outputArea.getDocument().getDefaultRootElement();
              Element first = root.getElement(0);
              outputArea.getDocument().remove(first.getStartOffset(), first.getEndOffset());
              outputArea.setCaretPosition(outputArea.getDocument().getLength());
          } catch (BadLocationException e) {
              ErrorHandler.handle(e);
          }
      }
  </code></pre>
  <p>My problem, however, comes when I try to remove the very last line (NOT OF TEXT I REPEAT NOT OF TEXT). This line could be anything appendable to a JTextPane such as a custom component, a string of text, an icon, etc. If anyone knows how to remove the last &quot;thing&quot; appended to a JTextPane, I would be ever-grateful to you.</p>
  <p>EDIT: add minimal Reproducible example:</p>
  <pre class="lang-java prettyprint-override"><code>import javax.swing.*;
  import javax.swing.text.*;
  
  public class Test {
      public static void main(String[] args) {
          new Test();
      }
  
      private static final String ELEM = AbstractDocument.ElementNameAttribute;
      private static final String ICON = StyleConstants.IconElementName;
      private static final String COMP = StyleConstants.ComponentElementName;
      private JTextPane outputArea;
  
      Test() {
          try {
              //init pane
              outputArea = new JTextPane();
  
              //insert component
              JTextField c = new JTextField(20);
              Style cs = outputArea.getStyledDocument().addStyle(&quot;name&quot;, null);
              StyleConstants.setComponent(cs, c);
              outputArea.getStyledDocument().insertString(outputArea.getStyledDocument().getLength(), &quot;string&quot;, cs);
  
              //new line
              println(&quot;&quot;);
  
              //add string
              println(&quot;this is a string added to the pane&quot;);
  
              //add image
              outputArea.insertIcon(new ImageIcon(&quot;/path/to/image.png&quot;));
  
              //new line
              println(&quot;&quot;);
  
              //before
              printContents();
  
              //----------------------------
              //call removeLastLine() as many times as needed and it should remove the last added &quot;thing&quot;
              //regardless of the order added (ex: component, text, icon should function the same as text, text, text, component obviously)
  
              //changes should be reflected here
              printContents();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public void removeLastLine() {
          //TODO remove last line of text, last component, last image icon, etc.
      }
  
      public void println(String Usage) {
          try {
              StyledDocument document = (StyledDocument) outputArea.getDocument();
              document.insertString(document.getLength(), Usage + &quot;\n&quot;, null);
              outputArea.setCaretPosition(outputArea.getDocument().getLength());
          }
  
          catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public void printContents() {
          try {
              ElementIterator iterator = new ElementIterator(outputArea.getStyledDocument());
              Element element;
              while ((element = iterator.next()) != null) {
                  System.out.println(element);
                  AttributeSet as = element.getAttributes();
                  if (as.containsAttribute(ELEM, ICON)) {
                      System.out.println(StyleConstants.getIcon(as).getClass());
                  }
                  else if (as.containsAttribute(ELEM, COMP)) {
                      System.out.println(StyleConstants.getComponent(as).getClass());
                  }
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>