How to disable type char in JTextArea but enable removed char (Backspace)

I have a simple application that limits the number of rows of the user. Calculation of the number of rows successfully performed. I use documentListener to capture the event from the user. But when the input from the user exceeds the specified number of rows, so I want to disable the user to input again. However, users still can remove the characters they enter. I've tried using setEditable (false), but this method resulted in JTextArea is not editable again permanently. This is my code. .... public Demo2() { initComponents(); textArea.getDocument().addDocumentListener(new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { System.out.println("Current Line when method call : "+getLineCountAsSeen(textArea)); if (getLineCountAsSeen(textArea) > maxLine){ JOptionPane.showMessageDialog(null, "Max line : "+maxLine); try { textArea.getDocument().remove(textArea.getDocument().getLength()-2, 2); } catch (BadLocationException ex) { Logger.getLogger(Demo2.class.getName()).log(Level.SEVERE, null, ex.getMessage()); } } else { lblLineCount.setText(String.valueOf(currentLine)); } } @Override public void removeUpdate(DocumentEvent e) { System.out.println("Current Line when method call : "+getLineCountAsSeen(textArea)); if (getLineCountAsSeen(textArea) > maxLine){ JOptionPane.showMessageDialog(null, "Max line : "+maxLine); try { textArea.getDocument().remove(textArea.getDocument().getLength()-2, 2); } catch (BadLocationException ex) { Logger.getLogger(Demo2.class.getName()).log(Level.SEVERE, null, ex); } } else { textArea.setEditable(true); lblLineCount.setText(String.valueOf(getLineCountAsSeen(textArea))); } } @Override public void changedUpdate(DocumentEvent e) { System.out.println("Current Line when method call : "+getLineCountAsSeen(textArea)); if (getLineCountAsSeen(textArea) > maxLine){ JOptionPane.showMessageDialog(null, "Max line : "+maxLine); try { textArea.getDocument().remove(textArea.getDocument().getLength()-2, 2); } catch (BadLocationException ex) { Logger.getLogger(Demo2.class.getName()).log(Level.SEVERE, null, ex); } } else { textArea.setEditable(true); lblLineCount.setText(String.valueOf(getLineCountAsSeen(textArea))); } } }); } public static int getLineCountAsSeen(JTextComponent txtComp) { Font font = txtComp.getFont(); FontMetrics fontMetrics = txtComp.getFontMetrics(font); int fontHeight = fontMetrics.getHeight(); int lineCount; try { int height = txtComp.modelToView(txtComp.getDocument().getEndPosition().getOffset() - 1).y; lineCount = height / fontHeight + 1; } catch (Exception e) { lineCount = 0; } if (lineCount == 0) { System.out.println("Not Set!"); return lineCount; } else { currentLine = lineCount; System.out.println("currentLine : "+currentLine); return currentLine; } } ....
For better help sooner, post an SSCCE.

以上就是How to disable type char in JTextArea but enable removed char (Backspace)的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏