如何知道是否在jtextarea中进行了任何更改? [英] How to know whether any changes in the jtextarea have been made or not?

查看:105
本文介绍了如何知道是否在jtextarea中进行了任何更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个jtextarea,用户可以在其中修改其内容.我想知道,如果有任何办法,用户在关闭应用程序之前是否已修改其内容.请帮忙.
-预先感谢

I've created a jtextarea where a user can modify its content. I want to know,if there is any way, whether the user has modified its content or not before closing the application. Please help.
-Thanks in advance

推荐答案

您需要添加

You need to add a DocumentListener to the Document that backs the text area.

然后在侦听器的回调方法(insertUpdate(),removeUpdate(),changedUpdate())中,只需设置一个已更改的标志并在关闭应用程序之前测试该标志

Then in the callback methods (insertUpdate(), removeUpdate(), changedUpdate()) of the listener, simply set a flag that something has changed and test that flag before closing the application


public class MyPanel
  implements DocumentListener
{
  private boolean changed;

  public MyPanel()
  {
    JTextArea textArea = new JTextArea();
    textArea.getDocument().addDocumentListener(this);
    .....
  }

  .....

  public void insertUpdate(DocumentEvent e)
  {
    changed = true;
  }
  public void removeUpdate(DocumentEvent e)
  {
    changed = true;
  }
  public void changedUpdate(DocumentEvent e)
  {
    changed = true;
  }
}

这篇关于如何知道是否在jtextarea中进行了任何更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆