Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a sample program, for you where, as you enter the fourth time into the TextPane it will beep, without even you pressing the <kbd>Enter</kbd> key:</p> <pre><code>import javax.swing.*; import javax.swing.text.*; import java.awt.Toolkit; public class TextPaneLimit extends JFrame { private JPanel panel; private JTextPane tpane; private AbstractDocument abDoc; public TextPaneLimit() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); panel = new JPanel(); tpane = new JTextPane(); Document doc = tpane.getStyledDocument(); if (doc instanceof AbstractDocument) { abDoc = (AbstractDocument)doc; abDoc.setDocumentFilter(new DocumentSizeFilter(3)); } panel.add(tpane); add(panel); pack(); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new TextPaneLimit().setVisible(true); } }); } } class DocumentSizeFilter extends DocumentFilter { private int max_Characters; private boolean DEBUG; public DocumentSizeFilter(int max_Chars) { max_Characters = max_Chars; DEBUG = false; } public void insertString(FilterBypass fb , int offset , String str , AttributeSet a) throws BadLocationException { if (DEBUG) { System.out.println("In DocumentSizeFilter's insertString method"); } if ((fb.getDocument().getLength() + str.length()) &lt;= max_Characters) super.insertString(fb, offset, str, a); else Toolkit.getDefaultToolkit().beep(); } public void replace(FilterBypass fb , int offset, int length , String str, AttributeSet a) throws BadLocationException { if (DEBUG) { System.out.println("In DocumentSizeFilter's replace method"); } if ((fb.getDocument().getLength() + str.length() - length) &lt;= max_Characters) super.replace(fb, offset, length, str, a); else Toolkit.getDefaultToolkit().beep(); } } </code></pre> <p>Hope this might help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload