Search code examples
javaswingjtextarea

Why is my textarea not updating?


I've looked at similar threads here on SO, but whatever I do, the jTextArea won't update.

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.

How can I make sure that the jTextArea is updated?

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.


Solution

  • 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.

    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. 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:

    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.