powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Java [игнор отключен] [закрыт для гостей] / Подскажите как в JTextArea ограничить размер вводимого текста
2 сообщений из 2, страница 1 из 1
Подскажите как в JTextArea ограничить размер вводимого текста
    #33535267
myinter@mail.ru
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Подскажите как в JTextArea ограничить размер вводимого текста
...
Рейтинг: 0 / 0
Подскажите как в JTextArea ограничить размер вводимого текста
    #33535311
myinter@mail.ru
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Все оказалось просто, главное уметь найти

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
 import  javax.swing.*;
 import  javax.swing.text.*;
 
 public   class  LimitedTextArea  extends  JFrame {
 
     public  LimitedTextArea() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JTextArea text =  new  JTextArea( 5 ,  40 );
        // Set the customized Document on the text area. Only allow
        // a maximum of ten characters:
        text.setDocument( new  LimitedDocument( 10 ));
        getContentPane().add( new  JScrollPane( text ));
        pack();
        setLocationRelativeTo( null );
    }
    
     public   static   void  main(String[] args) {
         new  LimitedTextArea().setVisible(true);
    }
    
    
    // Document that only allows a certain number of characters
     class  LimitedDocument  extends  PlainDocument {
 
         private   int  maxLength;
        
         public  LimitedDocument( int  maxLength) {
             this .maxLength = maxLength;
        }
 
        // This method is overriden from the super class. It will be called when
        // you are trying to insert text in your text component (by typing
        // or pasting).
         public   void  insertString( int  offs, String str, AttributeSet a)  throws  BadLocationException {
             int  currentLength = getLength();
             if ( currentLength >= maxLength ) {
                // There's not room for more characters. Return.
                 return ;
            }
             if ( currentLength + str.length() > maxLength ) {
                // All of the characters we are trying to insert will not fit.
                // We must trim the string.
                str = str.substring( 0 , maxLength - currentLength);
            }
            // Insert the text:
             super .insertString(offs, str, a);
        }
    }
    
}

...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / Java [игнор отключен] [закрыт для гостей] / Подскажите как в JTextArea ограничить размер вводимого текста
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]