在Swing的JTextPane中设置选项卡策略

时间:2023-01-27 16:33:51

I want my JTextPane to insert spaces whenever I press Tab. Currently it inserts the tab character (ASCII 9).

每当我按Tab键时,我希望我的JTextPane插入空格。目前它插入制表符(ASCII 9)。

Is there anyway to customize the tab policy of JTextPane (other than catching "tab-key" events and inserting the spaces myself seems an)?

反正是有自定义JTextPane的选项卡策略(除了捕获“tab-key”事件并插入空格本身似乎)?

3 个解决方案

#1


5  

You can set a javax.swing.text.Document on your JTextPane. The following example will give you an idea of what I mean :)

您可以在JTextPane上设置javax.swing.text.Document。以下示例将让您了解我的意思:)

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;

public class Tester {

    public static void main(String[] args) {
        JTextPane textpane = new JTextPane();
        textpane.setDocument(new TabDocument());
        JFrame frame = new JFrame();
        frame.getContentPane().add(textpane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(200, 200));
        frame.setVisible(true);
    }

    static class TabDocument extends DefaultStyledDocument {
        @Override
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
            str = str.replaceAll("\t", " ");
            super.insertString(offs, str, a);
        }
    }
}

Define a DefaultStyleDocument to do the work. Then set the Document to your JTextPane.

定义DefaultStyleDocument来完成工作。然后将Document设置为JTextPane。

Cheers Kai

#2


0  

As far as I know, you'd have to catch key events, as you say. Depending on usage, you might also get away with waiting until the input is submitted, and changing tabs to spaces at that time.

据我所知,正如你所说,你必须抓住关键事件。根据使用情况,您可能还会等待提交输入,并在此时将标签更改为空格。

#3


0  

You could try sub-classing DefaultStyledDocument and overriding insert to replace any tabs in the inserted elements with spaces. Then install your sub-class in JTextPane with setStyledDocument(). This may be more trouble than catching key events though.

您可以尝试对DefaultStyledDocument进行子类化并覆盖insert以使用空格替换插入元素中的任何选项卡。然后使用setStyledDocument()在JTextPane中安装子类。这可能比捕捉关键事件更麻烦。

#1


5  

You can set a javax.swing.text.Document on your JTextPane. The following example will give you an idea of what I mean :)

您可以在JTextPane上设置javax.swing.text.Document。以下示例将让您了解我的意思:)

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;

public class Tester {

    public static void main(String[] args) {
        JTextPane textpane = new JTextPane();
        textpane.setDocument(new TabDocument());
        JFrame frame = new JFrame();
        frame.getContentPane().add(textpane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(200, 200));
        frame.setVisible(true);
    }

    static class TabDocument extends DefaultStyledDocument {
        @Override
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
            str = str.replaceAll("\t", " ");
            super.insertString(offs, str, a);
        }
    }
}

Define a DefaultStyleDocument to do the work. Then set the Document to your JTextPane.

定义DefaultStyleDocument来完成工作。然后将Document设置为JTextPane。

Cheers Kai

#2


0  

As far as I know, you'd have to catch key events, as you say. Depending on usage, you might also get away with waiting until the input is submitted, and changing tabs to spaces at that time.

据我所知,正如你所说,你必须抓住关键事件。根据使用情况,您可能还会等待提交输入,并在此时将标签更改为空格。

#3


0  

You could try sub-classing DefaultStyledDocument and overriding insert to replace any tabs in the inserted elements with spaces. Then install your sub-class in JTextPane with setStyledDocument(). This may be more trouble than catching key events though.

您可以尝试对DefaultStyledDocument进行子类化并覆盖insert以使用空格替换插入元素中的任何选项卡。然后使用setStyledDocument()在JTextPane中安装子类。这可能比捕捉关键事件更麻烦。