1

I have two methods: colorText() and colorBackground(). They both color the selected text fromjTextPane, either its foreground or background. Now I would like to have another method that would set the whole text back to normal (with black foreground and white background).
How do I do that?

Himanshu
  • 4,269
  • 16
  • 29
  • 37
lol
  • 231
  • 1
  • 10

1 Answers1

2

Given a StyledDocument, as shown here, you can invoke setCharacterAttributes() with whatever style you want. The example below uses a default SimpleAttributeSet, but you can use any AttributeSet, such as the ones shown here.

before after

f.add(new JButton(new AbstractAction("Clear") {

    @Override
    public void actionPerformed(ActionEvent e) {
        doc.setCharacterAttributes(0, doc.getLength(), new SimpleAttributeSet(), true);
    }
}), BorderLayout.SOUTH);
Community
  • 1
  • 1
trashgod
  • 200,320
  • 28
  • 229
  • 974