Results 1 to 5 of 5
  1. #1
    matthew712 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default Thread updates invisible GUI

    Hey guys,

    I would really appreciate it if someone could please help me out with this problem. It occurs in a program of mine, however I've re-created it in a sandbox to demonstrate it better.

    I've got a thread which runs in the background which I want to update the jTextArea on certain events (replaced by buttons in this example).

    The problem is when the thread updates the jTextArea nothing happens, yet if I run the same code without the thread it works perfectly.

    I realised after a while by adding print statements to the thread that an invisible jTextArea was being updated instead.

    Please could someone advise me how I would go about updating the visible jTextArea from the thread.

    Thank you in advance,

    Matthew



    Thread test output
    jTextArea: (None)
    Console: THREAD TEST START
    THREAD TEST END
    Thread 'jTextArea1' print: Thread test 1Thread test 2

    Non-thread test output
    jTextArea: Non-thread test 1Non-thread test 2
    Console: NON-THREAD TEST START
    NON-THREAD TEST END
    Non-thread 'jTextArea1' print: Non-thread test 1Non-thread test 2

    Java Code:
    
    public class example extends javax.swing.JFrame implements Runnable
    {
    
        public static void main(String args[])
        {
            java.awt.EventQueue.invokeLater(new Runnable()
            {
    
                public void run()
                {
                    new example().setVisible(true);
                }
            });
        }
    
        public example()
        {
            initComponents();
        }
    
        public void run()
        {
            System.out.println("THREAD TEST START");
            jTextArea1.append("Thread test 1");
            jTextArea1.append("Thread test 2");
            System.out.println("THREAD TEST END");
            System.out.println("Thread 'jTextArea1' print: " + jTextArea1.getText());
        }
    
        private void threadTestActionPerformed(java.awt.event.ActionEvent evt)                                           
        {                                               
            Thread t1 = new Thread(new example());
            t1.start();
        }                                          
    
        private void nonThreadTestActionPerformed(java.awt.event.ActionEvent evt)                                              
        {                                                  
            System.out.println("NON-THREAD TEST START");
            jTextArea1.append("Non-thread test 1");
            jTextArea1.append("Non-thread test 2");
            System.out.println("NON-THREAD TEST END");
            System.out.println("Non-thread 'jTextArea1' print: " + jTextArea1.getText());
        }                                             
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jScrollPane1 = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();
            threadTest = new javax.swing.JButton();
            nonThreadTest = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jTextArea1.setColumns(20);
            jTextArea1.setRows(5);
            jScrollPane1.setViewportView(jTextArea1);
    
            threadTest.setText("Thread test");
            threadTest.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    threadTestActionPerformed(evt);
                }
            });
    
            nonThreadTest.setText("Non-thread test");
            nonThreadTest.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    nonThreadTestActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap(93, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(threadTest)
                            .addGap(29, 29, 29)
                            .addComponent(nonThreadTest)
                            .addGap(80, 80, 80))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(111, 111, 111))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(58, 58, 58)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(43, 43, 43)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(threadTest)
                        .addComponent(nonThreadTest))
                    .addContainerGap(80, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
        // Variables declaration - do not modify                     
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        private javax.swing.JButton nonThreadTest;
        private javax.swing.JButton threadTest;
        // End of variables declaration                   
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Here:
    Java Code:
    Thread t1 = new Thread(new example());

    You are passing an example object into the Thread, but you must realize that since it is a new object, it is completely distinct from the example object that is being displayed. If you want to have your code have an effect on the example object being displayed, then this is the object that must be passed into this constructor:

    Java Code:
    Thread t1 = new Thread(this);

    Is this clear to you, because if not speak up as this is a critical point.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    A couple other points:

    1) Although the JTextArea API states that you can call append on a JTextArea object in a background thread, that this method is thread-safe, experience has shown that this is not true. You should take care to call this and most other Swing methods on the main Swing thread, the EDT. If you are unfamiliar with the EDT, please read this important tutorial: Concurrency in Swing

    2) Please learn and abide by Java's naming conventions as doing this will help others (including us, your instructors, and your bosses) to better understand your code. Find out more here: Java Coding Conventions

  4. #4
    matthew712 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    Thank you so much for helping me I really appreciate it

    You answered my question perfectly and my program now works as I wanted

    Also thank you for your instant reply, clear explanations and advice which I will definitely take onboard

    Thanks again,

    Matthew

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Automatic Updates...How to stop them?
    By Otto in forum New To Java
    Replies: 5
    Last Post: 05-26-2010, 09:18 AM
  2. Java JDK updates on Mac?
    By QualityControl in forum New To Java
    Replies: 4
    Last Post: 03-24-2010, 05:57 PM
  3. Replies: 2
    Last Post: 08-02-2009, 12:54 PM
  4. Multiple Queries using batch Updates
    By swati.jyoti in forum JDBC
    Replies: 3
    Last Post: 05-12-2009, 11:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •