我一直想知道是否可以在没有按钮点击的情况下根据jtextarea内的输入更新JFrame或JDialogs。例如,在我输入textarea中的一些文本之后。它应该自动更新jlabel而无需按钮。我有搜索troughout但我找到的所有信息仅基于按钮点击。举个例子,
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(0, 1));
JTextArea input = new JTextArea();
JLabel output = new JLabel("test");
// Condition
// If user input "abc" inside textfield
// JLabel will automatically display "abc"
frame.add(input);
frame.add(output);
frame.setSize(300,400);
frame.setVisible(true);
我需要刷新整个框架吗?它会影响用户已经填充回空的所有其他文本字段吗?
谢谢
Document
包含的JTextArea
对象接收更新。
JTextArea input = new JTextArea();
input .getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {}
@Override
public void insertUpdate(DocumentEvent e) {}
@Override
public void changedUpdate(DocumentEvent arg0) {
//Add logic here to check if particular word is entered.
//if yes show label, else hide the label object
}
});