Enforce max characters on Swing JTextArea with a few curve balls

前端 未结 3 838
南笙
南笙 2021-01-16 04:48

I\'m trying to add functionality to a Swing JLabel and JTextArea such that:

  • The user is only allowed to enter 500 characters into the textarea (max)
  • T
相关标签:
3条回答
  • 2021-01-16 05:26

    evt.consume(); will help alot in this case..you dont need to use DocumentFilter. here is a much easier way of limiting user at certain length

    private void jTextArea1KeyTyped(java.awt.event.KeyEvent evt) {                                    
        String s=jTextArea1.getText();
       int l=s.length();
       jTextField1.setText(String.valueOf(l));
       int i=10-l;
       jTextField2.setText(String.valueOf(i));
       try{
       if(l>=10){evt.consume();
       }
       }
       catch(Exception w){}
    
    } 
    
    0 讨论(0)
  • 2021-01-16 05:34

    To add on to what Ray S. Kan said:

    There is an easier way to limit the characters for a JTextArea without having to use a DocumentFilter as shown by Ray S. Kan, but the issue with his answer is that it does not prevent someone from pasting in a long text. The following will prevent a user from pasting in stuff to bypass the limit:

    @Override
    public void keyTyped(KeyEvent e) {
        int max = 25;
        if(text.getText().length() > max+1) {
            e.consume();
            String shortened = text.getText().substring(0, max);
            text.setText(shortened);
        }else if(text.getText().length() > max) {
            e.consume();
        }
    }
    

    This will stop a key from being pressed if the length is not pass max, but if it passes max, it will simply replace the string in the text area with a shorter string. The text variable is the JTextArea swing object.

    0 讨论(0)
  • 2021-01-16 05:38

    You can limit the max size by using a DocumentFilter, check this documentation section, it has a working example of what you need.

    Take this as an example, I used the component from the example file above:

    import java.awt.BorderLayout;
    
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    
    import components.DocumentSizeFilter;
    
    public class Test {
    
        public static void main(String[] args) {
            new TestFrame().setVisible(true);
        }
    
        private static class TestFrame extends JFrame{
            private JTextField textField;
            private DefaultStyledDocument doc;
            private JLabel remaningLabel = new JLabel();
    
            public TestFrame() {
                setLayout(new BorderLayout());
    
                textField = new JTextField();
                doc = new DefaultStyledDocument();
                doc.setDocumentFilter(new DocumentSizeFilter(500));
                doc.addDocumentListener(new DocumentListener(){
                    @Override
                    public void changedUpdate(DocumentEvent e) { updateCount();}
                    @Override
                    public void insertUpdate(DocumentEvent e) { updateCount();}
                    @Override
                    public void removeUpdate(DocumentEvent e) { updateCount();}
                });
                textField.setDocument(doc);
    
                updateCount();
    
                add(textField, BorderLayout.CENTER);
                add(remaningLabel, BorderLayout.SOUTH);
    
                setLocationRelativeTo(null);
                pack();
            }
    
            private void updateCount()
            {
                remaningLabel.setText((500 -doc.getLength()) + " characters remaining");
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题