首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在另一个DefaultStyledDocument中插入一个DefaultStyledDocument

在另一个DefaultStyledDocument中插入一个DefaultStyledDocument
EN

Stack Overflow用户
提问于 2010-04-17 03:57:58
回答 1查看 280关注 0票数 1

我想在另一个DefaultStyledDocument中插入一个DefaultStyledDocument。我该怎么做呢?我知道这个方法:

代码语言:javascript
复制
AbstractDocument.insertString(int offs,
                         String str,
                         AttributeSet a)

我真正想要的是这样的东西:

代码语言:javascript
复制
DefaultStyledDocument.insertDocument(int offs,
                         AbstractDocument doc)

有没有办法做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2011-08-07 09:50:30

使用PlainDocument时使用 JTextField | JTextArea

示例

代码语言:javascript
复制
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

@SuppressWarnings("serial")
public class UserPlainDocument extends PlainDocument {

    private final int LIMIT_OF_CHARS;
    private final int DATA_TYPE;
    private final char[] SKIPPING_CHARS;

    public final static int ALL_DATA_TYPES = 1;
    public final static int DATA_TYPE_OF_INTEGER = 2;
    public final static int DATA_TYPE_OF_DOUBLE = 3; 

    public UserPlainDocument(int limitOfChars, int dataType) {
        if(4 < dataType || 0 > dataType) throw new IllegalArgumentException(
               "This dataType value not available " +
               "please check the value.");          
        this.LIMIT_OF_CHARS = limitOfChars;
        this.DATA_TYPE = dataType;
        this.SKIPPING_CHARS = null;
    }

    public UserPlainDocument(int limitOfChars, char[] skippingChars) {
        this.LIMIT_OF_CHARS = limitOfChars;
        this.SKIPPING_CHARS = skippingChars;
        this.DATA_TYPE = 0;
    }

    @Override
    public void insertString(int offs, String charAt, AttributeSet set)
            throws BadLocationException {
        if(offs + charAt.length() <= LIMIT_OF_CHARS)
            try {
                switch (DATA_TYPE) {
                    case DATA_TYPE_OF_INTEGER:
                        Integer.parseInt(charAt);
                        super.insertString(offs, charAt, set);
                        break;

                    case DATA_TYPE_OF_DOUBLE:
                        if(charAt.equals(".") && getText(0, offs).
                          indexOf(".") == -1) {
                            super.insertString(offs, charAt, set);
                            break;
                        }
                        Double.parseDouble(charAt);
                        super.insertString(offs, charAt, set);
                        break;

                    case ALL_DATA_TYPES:
                        super.insertString(offs, charAt, set);
                        break;

                    default:
                        for (int i = 0; i < SKIPPING_CHARS.length; i++) 
                            if(charAt.equals(String.valueOf(SKIPPING_CHARS[i])))  
                                throw new BadLocationException("", offs);

                        super.insertString(offs, charAt, set);
                        break;

                }
            } catch (NumberFormatException e) {
                throw new BadLocationException(e.getMessage(), offs); 
            }
        else
            throw new BadLocationException("", offs);
    }       
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2655756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档