如何知道jtextarea是否有任何变化?(How to know whether any changes in the jtextarea have been made or not?)

编程入门 行业动态 更新时间:2024-10-23 15:21:22
如何知道jtextarea是否有任何变化?(How to know whether any changes in the jtextarea have been made or not?)

我创建了一个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

最满意答案

您需要将DocumentListener添加到支持文本区域的Document 。

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

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

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

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

更多推荐

本文发布于:2023-08-06 20:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有任何   jtextarea

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!