Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>for listening changes into JTextComponent is there DocumentListener, if you have to need control over inputed Char, sings, whitespace chars or word(s) you have to implements DocumentFilter</p> <p>notice for Chars reservated by programing language(s) you have to use double escapes, </p> <p><code>\\(</code> instead of <code>(</code> </p> <p>or </p> <p><code>\\{</code> instead of <code>{</code> </p> <p>otherwise you get </p> <pre><code>Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Illegal repetition </code></pre> <p>for example</p> <pre><code>import java.awt.*; import java.util.ArrayList; import java.util.List; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; public class TextAreaTest extends JFrame { private static final long serialVersionUID = 1L; private JTextArea textArea; public TextAreaTest() { textArea = new JTextArea(); textArea.setPreferredSize(new Dimension(60, 32)); textArea.setOpaque(true); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); ((AbstractDocument) textArea.getDocument()).setDocumentFilter(new DocumentFilter() { @Override public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { string = string.replaceAll("\\{", "\\{}"); super.insertString(fb, offset, string, attr); } @Override public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { text = text.replaceAll("\\{", "\\{}"); //TODO must do something here super.replace(fb, offset, length, text, attrs); } }); textArea.getDocument().addDocumentListener(new DocumentListener() { @Override public void changedUpdate(DocumentEvent e) { update(e); } @Override public void insertUpdate(DocumentEvent e) { update(e); } @Override public void removeUpdate(DocumentEvent e) { update(e); } private void update(DocumentEvent e) { List&lt;String&gt; lines = getLines(textArea); String lastLine = lines.get(lines.size() - 1); int tabbedTextWidth = Utilities.getTabbedTextWidth(new Segment( lastLine.toCharArray(), 0, lastLine.length()), textArea.getFontMetrics(textArea.getFont()), 0, null, 0); int lineHeight = getLineHeight(textArea); if (lines.size() * lineHeight &gt; textArea.getHeight() || tabbedTextWidth &gt; textArea.getWidth()) { System.out.println("Too big! Should refuse the update!"); } } }); getContentPane().add(textArea); } private static List&lt;String&gt; getLines(JTextArea textArea) { int lineHeight = getLineHeight(textArea); List&lt;String&gt; list = new ArrayList&lt;String&gt;(); for (int num = 0;; num++) { int i = textArea.viewToModel(new Point(0, num * lineHeight)); int j = textArea.viewToModel(new Point(0, (num + 1) * lineHeight)); if (i == 0 &amp;&amp; j == 0) { continue; } if (textArea.getDocument().getLength() == i &amp;&amp; i == j) { break; } String s = removeTrailingNewLine(textArea.getText().substring(i, j)); list.add(s); //System.out.println(i + " " + j + " = " + s); } return list; } private static int getLineHeight(JTextArea textArea) { return textArea.getFontMetrics(textArea.getFont()).getHeight(); } private static String removeTrailingNewLine(String s) { if (s.endsWith("\n")) { return s.substring(0, s.length() - 1); } else { return s; } } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { TextAreaTest test = new TextAreaTest(); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test.pack(); test.setVisible(true); } }); } } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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