简体   繁体   English

为什么我的文本区域没有更新?

[英]Why is my textarea not updating?

I've looked at similar threads here on SO, but whatever I do, the jTextArea won't update. 我在SO上看过类似的线程,但是无论我做什么, jTextArea都不会更新。

If I try something like: 如果我尝试以下操作:

for (int i = 0; i < a.getB().getC().size(); i++) {
        jTextArea1.append(a.getB().getC(i).toString()); 
        jTextArea1.update(jTextArea1.getGraphics());
    } 

I saw someone using getGraphics to resolve this problem, but it is not working in my case. 我看到有人使用getGraphics解决此问题,但在我的情况下它不起作用。

How can I make sure that the jTextArea is updated? 如何确定jTextArea已更新?

I've made sure that there's a value in the string as I print that to the console before trying to append it to the textarea. 在尝试将其附加到textarea之前,在将其打印到控制台时,我已经确保字符串中有一个值。

Well, you should definitely not call update with the graphics of the textarea. 好吧,您绝对不应该使用文本区域的图形来调用更新。 Check this in order to understand update/getGraphics. 检查为了了解更新/ getGraphics。

Check this for a solution: JTextArea's append () method doesn't seem to work 检查此解决方案: JTextArea的append()方法似乎不起作用

If it does not help, try to debug, perhaps you are appending an empty string or something like this. 如果没有帮助,请尝试调试,也许您要附加一个空字符串或类似的内容。

Swing Components should be updated in EDT. Swing组件应在EDT中更新

for (int i = 0; i < a.getB().getC().size(); i++) {

final int value = i;

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {

        jTextArea1.append(a.getB().getC(value).toString()); 
    }
});
}

jTextArea.append should work. jTextArea.append应该可以工作。 Maybe the update to the text area is blocked by some GUI action in the Swing Thread? 也许对文本区域的更新被Swing线程中的某些GUI操作阻止了?

Apparantly the problem was that I created 2 instances of the GUI and one wasn't visible, so it would always exist behind the GUI that I could see, thus hiding the real problem. 显然,问题是我创建了2个GUI实例,而一个实例是不可见的,因此它始终存在于我可以看到的GUI后面,从而隐藏了真正的问题。

When I found that out the problem was easy to solve. 当我发现问题很容易解决时。 In my parentGUI where I create the other gui's, I now gave these new GUI's an instance of the parentGUI. 在我创建其他GUI的parentGUI中,现在给这些新的GUI一个parentGUI的实例。 See: 看到:

private void dropdownActionPerformed(java.awt.event.ActionEvent evt) {                                             
    if (dropdown.getSelectedIndex() == 1) {
        NewFrame newFrame = new NewFrame(this); //the keyword this did the trick
        newFrame.setVisible(true);
    }
}

Then in the constructor of NewFrame I catch the instance of the parentGUI: 然后在NewFrame的构造函数中,我捕获了parentGUI的实例:

public class NewFrame extends javax.swing.JFrame {

private GUI parentgui;

public NewFrame(GUI parentGUI) {
    initComponents();   
    parentgui = parentGUI;
}

And now our problem is solved :) I have only one instance of the parentGUI, and the jTextArea gets filled perfectly. 现在,我们的问题得以解决:)我只有一个parentGUI实例,并且jTextArea得到了完美填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM