问题
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%.
回答1:
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
来源:https://stackoverflow.com/questions/12859751/how-to-remove-component-from-jtextpane