1

OK. Inserting a component programatically is obvious: myJTextPane.insertComponent.

Accessing components was a little trickier, but I use something like: myJTextPane.getComponents().getComponents()[0]. (1)

But how do I remove a component programatically from myJTextPane?

(1) I am actually programming in Clojure, so the syntax may not be 100%.

Terje Dahl
  • 942
  • 10
  • 20

1 Answers1

4

You treat it as a character at a specific position:

myJTextPane.getDocument().remove(int offs, int len)

For example if you have a text pane with components in this order:

[Component1] - [Component2] - [Component3] - some text

and you want to remove 2nd and 3rd components:

myJTextPane.getDocument().remove(1, 2)

See documentation

Rempelos
  • 1,220
  • 10
  • 18
  • It is quick-and-dirty. It doesn't take into conciseration that the user might have typed characters into the field as well. But it is good enough for now. Thanks. – Terje Dahl Oct 14 '12 at 06:32