Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main( String[] args ) throws IOException{
- JFrame frame = new JFrame( "Test Format" );
- frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
- JTextField field = new JTextField();
- class LimitedTextFieldDocument extends PlainDocument{
- private int limit;
- public LimitedTextFieldDocument( int limit ) {
- setLimit(limit);
- }
- @Override
- public void insertString(int offs, String str, AttributeSet a) throws BadLocationException{
- if( getLength() + str.length() <= getLimit() )
- str = str.substring( 0, getLimit() - getLength() );
- super.insertString(offs, str, a);
- }
- public int getLimit() {
- return limit;
- }
- public void setLimit(int limit) {
- this.limit = limit;
- }
- }
- field.setDocument( new LimitedTextFieldDocument( 5 ) );
- frame.add( field );
- frame.pack();
- frame.setVisible( true );
- frame.setLocationRelativeTo( null );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement