code_junkie

Setting the tab policy in Swing's JTextPane

Nov 12th, 2011
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. import java.awt.Dimension;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JTextPane;
  5. import javax.swing.text.AttributeSet;
  6. import javax.swing.text.BadLocationException;
  7. import javax.swing.text.DefaultStyledDocument;
  8.  
  9. public class Tester {
  10.  
  11. public static void main(String[] args) {
  12. JTextPane textpane = new JTextPane();
  13. textpane.setDocument(new TabDocument());
  14. JFrame frame = new JFrame();
  15. frame.getContentPane().add(textpane);
  16. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17. frame.setSize(new Dimension(200, 200));
  18. frame.setVisible(true);
  19. }
  20.  
  21. static class TabDocument extends DefaultStyledDocument {
  22. @Override
  23. public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
  24. str = str.replaceAll("t", " ");
  25. super.insertString(offs, str, a);
  26. }
  27. }
  28. }
Add Comment
Please, Sign In to add comment