Java GUI themes and updates

I have two classes, one called GUIFrame that contains all the graphics and a threading class called squeak . There are two graphical elements in the GUIFrame : the first button, which, when pressed, starts a thread:

futures.put(1, pool.submit(new squeak("SqueakyThread",this.jTextArea1)));

      

The second is javax.swing.JTextArea with the jTextArea1 variable .

The second class mentioned above is a thread class called squeak (which implements Runnable) and contains a while (true) loop . This whole class generates a random number between 1 and 10 and then outputs that number to jTextArea1 in the GUIFrame class .

Question: Is this the correct formal way to update a GUI element in Java?

I realize this question has been asked a lot, but with all the different answers out there, I hope this can provide a simple pattern for me and others.

GUIFrame.java

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


public class GUIFrame extends javax.swing.JFrame {

    public GUIFrame() {
        this.pool = Executors.newCachedThreadPool();
        initComponents();
    }

    private ExecutorService pool;
    private Map<Integer, Future<?>> futures = new HashMap<>();
    /**
     * 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() {

        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Start");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextArea1.setEditable(false);
        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        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()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 468, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE))
                .addContainerGap())
        );

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        futures.put(1, pool.submit(new squeak("SqueakyThread",this.jTextArea1)));
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* 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(GUIFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUIFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUIFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUIFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GUIFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration                   
}

      

squeak.java

import java.util.Random;
import java.util.concurrent.TimeUnit;
import javax.swing.SwingUtilities;

public class squeak implements Runnable {

    private  String Type = "";
    private  javax.swing.JTextArea MW;

    squeak (String type, javax.swing.JTextArea MW)
    {
        this.Type = type;
        this.MW = MW;
    }

    @Override
    public void run ()
    {

        while(true)
        {
            UpdateGUI(RandomNumber()+"\r\n");

            try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e)
            {
                UpdateGUI("Thread is now Exiting!\r\n");
                //Return cause program to exit the while(true) loop and end
                return;
            }
        }
    }

    private int RandomNumber(){
        Random r = new Random();
        int num = r.nextInt(10-1) + 1;
        return num;
    }


    private void UpdateGUI(final String foo) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                // Here, we can safely update the GUI
                // because we'll be called from the
                // event dispatch thread
                MW.append(foo);
            }
        });
    }
}            

      

+1


source to share


1 answer


Relatively

private void updateGUI(final String foo) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            // Here, we can safely update the GUI
            // because we'll be called from the
            // event dispatch thread
            MW.append(foo);
        }
    });
}  

      

Yes, this is definitely one way to make code in a non-Swing event flow to update a Swing component - by queuing a Runnable on on the Swing event flow queue via SwingUtilities.invokeLater(new Runnable() {...})

.

Another way, and my preferred way, is to use SwingWorker, because it has built-in mechanisms to allow code to run in a background thread and is still able to safely make Swing calls on the Swing event thread.



My first criticism (and I must find something to criticize, right?) Is that your code must follow Swing naming conventions. For example, variable names must begin with a lowercase letter, not an uppercase letter. This is not so important if you are just writing the code for your own pleasure, but it is very important if you want others to review or update or save your code.

My second criticism is here:

    jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseReleased(java.awt.event.MouseEvent evt) {
            jButton1MouseReleased(evt);
        }
    });

      

You shouldn't use MouseListeners for JButtons as it gets too low. Use ActionListeners, as that's what they are created for and much safer. For example, if your code disables an Action button or its model, the button should not respond, but your code will behave incorrectly and will not be disabled correctly.

+1


source







All Articles