gpt4 book ai didi

java - 如何在 JTextArea 中禁用类型 char 但启用删除的字符(退格键)

转载 作者:行者123 更新时间:2023-12-02 06:54:01 25 4
gpt4 key购买 nike

我有一个简单的应用程序,它限制用户的行数。成功执行行数的计算。我使用 documentListener 来捕获用户的事件。

但是当用户输入超过指定的行数时,所以我想禁止用户再次输入。但是,用户仍然可以删除他们输入的字符。

我尝试过使用 setEditable (false),但此方法导致 JTextArea 永久无法再次编辑。

这是我的代码。

....
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;
}
}
....

最佳答案

尝试使用 DocumentFilter 而不是 DocumentListener 之类的东西

 final AbstractDocument abstractDocument = (AbstractDocument) textArea.getDocument();

abstractDocument.setDocumentFilter(new DocumentFilter()
{
@Override
public void remove(final FilterBypass fb, final int offset, final int length) throws BadLocationException
{
super.remove(fb, offset, length);
}

@Override
public void insertString(final FilterBypass fb,
final int offset,
final String string,
final AttributeSet attr) throws BadLocationException
{

if (getLineCountAsSeen(textArea) < 4)
{
super.insertString(fb, offset, string, attr);
}
}

@Override
public void replace(final FilterBypass fb,
final int offset,
final int length,
final String text,
final AttributeSet attrs) throws BadLocationException
{
if (getLineCountAsSeen(textArea) < 4)
{
super.replace(fb, offset, length, text, attrs);
}
}
});

关于java - 如何在 JTextArea 中禁用类型 char 但启用删除的字符(退格键),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17640225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com