如何在 Java Swing 中更新 JTextArea?
我有一个名为“jComboBox18”的 JComboBox 和一个名为“jTextArea11”的 JTextArea。现在我希望每当从“jComboBox18”组合框中选择一个项目时,其相应的描述就会显示在“jTextArea11”文本区域中。
我已向 JComboBox 添加了适当的侦听器,但 JTextArea 未显示任何文本。我编写的代码如下:
private void jComboBox18ItemStateChanged(java.awt.event.ItemEvent evt) {
Object item = jComboBox18.getSelectedItem();
if(item != null) {
ems.logic.Process selectedProcess = (ems.logic.Process)item;
jTextArea11.setText(selectedProcess.getProcessDescription());
jTextArea11.updateUI();
jTextArea11.revalidate();
jTextArea11.validate();
}
}
=======================已编辑================== =========================
该方法肯定被调用。我正在更改另一个组合框的状态 这也是在此方法中编写的,只要从“jComboBox18”中选择项目,其状态就会成功更改
I have a JComboBox named "jComboBox18" and a JTextArea "jTextArea11". Now I want that whenever a item is selected from the "jComboBox18" combo box its corresponding description is shown in the "jTextArea11" textarea.
I have added the appropriate listener to the JComboBox But the JTextArea is not showing any text. The code that I have written is as follows:
private void jComboBox18ItemStateChanged(java.awt.event.ItemEvent evt) {
Object item = jComboBox18.getSelectedItem();
if(item != null) {
ems.logic.Process selectedProcess = (ems.logic.Process)item;
jTextArea11.setText(selectedProcess.getProcessDescription());
jTextArea11.updateUI();
jTextArea11.revalidate();
jTextArea11.validate();
}
}
=====================EDITED===========================================
The method is being called for sure. I am changing the state of one more combobox
which is also being written in this method and its state changes successfully whenever item is selected from the "jComboBox18"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这应该有效。事实上,您应该只需要 setText() 调用。我的猜测是,由于某种原因,您的函数没有被调用。在代码中放置一个断点并确保它被调用。
I think That should work. In fact, you should only need the setText() call. My guess is that you're function isn't getting called for some reason. Put a break point in your code and make sure it's getting called.
在显示的代码中,您的方法被命名为
jComboBox18ItemStateChanged
。您确定正在调用此方法吗? JComboBox 的 ItemListener 应实现接口 ItemListener,该接口声明子类应实现以下方法。您如何将 ItemListener 的实例添加到 JComboBox 中?
编辑:
阅读您的编辑和评论后,我能想到的另一种可能性是:
您有一个侦听器,当文本区域更新时会触发该侦听器,并且可能会撤消 JComboBox 侦听器中所做的更改。
In the code shown your method is named as
jComboBox18ItemStateChanged
. Are you sure this method is being called. The ItemListener for a JComboBox should implement the interface ItemListener which declares that the subclasses should implement the below method.How are you adding an instance of ItemListener to your JComboBox ?
EDIT:
After reading your edit and comments one another possiblity that I can think of is that:
you have a listener that is triggered when the textarea is updated and probably itis undoing the changes done in JComboBox listener.