2010-04-16 4 views
1

DefaultStyledDocument을 다른 DefaultStyledDocument에 삽입하고 싶습니다. 어떻게해야합니까? 나는이 방법을 알고하나의 DefaultStyledDocument를 다른 DefaultStyledDocument에 삽입하십시오.

DefaultStyledDocument.insertDocument(int offs, 
         AbstractDocument doc) 

이 일을하는 방법이 있나요 :

AbstractDocument.insertString(int offs, 
         String str, 
         AttributeSet a) 

이 같은 내가 정말 원하는 무엇입니까?

+0

당신이 원하는 것은 당신의 DefaultStyleDocument를 복제하는 것입니다? –

+0

하나의 DefaultStyledDocument를 다른 DefaultStyledDocument에 삽입하고 싶습니다. 한 트리를 다른 트리에 삽입하고 싶습니다. –

답변

1

JTextField |를 사용하여 PlainDocument를 사용합니다. JTextArea에

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); 
    }  
} 
관련 문제