Greyshack
Greyshack

Reputation: 1971

JTextArea is not updating from another class

I have a JFrame class:

public class Console extends javax.swing.JFrame {


private StringBuilder b;
/**
 * Creates new form NewJFrame
 */
public Console() {
    initComponents();
    b = new StringBuilder();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setName("MainFrame"); // NOI18N
    setResizable(false);

    jScrollPane1.setHorizontalScrollBar(null);
    jScrollPane1.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));
    jScrollPane1.getViewport().setBorder(null);
    jScrollPane1.setViewportBorder(null);
    jScrollPane1.setBorder(null);


    jTextArea1.setBackground(new java.awt.Color(0, 0, 0));
    jTextArea1.setColumns(20);
    jTextArea1.setForeground(new java.awt.Color(204, 204, 204));
    jTextArea1.setRows(5);
    jTextArea1.setText("initial text\n");
    jTextArea1.setEditable(false);
    jScrollPane1.setViewportView(jTextArea1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
    );

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */
public void start() {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Console.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Console.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Console.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Console.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Console().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration       

public void addText(String s){
    jTextArea1.append(s);
    System.out.println(jTextArea1.getText()+ " = text");
}
public void consoleText(final String consoleUpdate){
     SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          jTextArea1.append(consoleUpdate);
        }
     });

}

}

and I have an instance of this class in another class:

public SocketServer(int port){
    this.port = port;
    clientList = new ArrayList<Socket>();
    console = new Console();
    console.start();

This doesn't work and I dont know why, I'm calling it later on in the SockerServer.

SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          console.addText("Starting server at port: "+port);
        }
      });
}

Simple

console.addText(...);

doesn't work either. I mean it doesn't show, but it does add to JTextArea, because I'm loging the value immediately after and in eclipse console the text is appended to JTextArea, only never shown.

Upvotes: 0

Views: 137

Answers (1)

camickr
camickr

Reputation: 324088

In your Console class you use:

 new Console().setVisible(true);

So this creates the Console and displays the frame.

Then in your server code you do:

console = new Console();

which creates a second Console. So your server class is updating the text area of the second invisible Console.

You should only ever have a single Console. If you want your server class to update the Console then you need to pass a reference of the Console class to your server instead of creating a new Console. Something like:

Server server = new Server(console);

now your Server class has a reference to the visible Console.

Upvotes: 1

Related Questions