Search code examples
javamultithreadingjbutton

How to start and stop a threads in netbeans from a jButton click


I write Java desktop app to fetch and post some data from my online rails backend app. The App have to call a get request every 5 second to update the relay state(example Arduino). here is my code:

public class GUI extends javax.swing.JFrame {

private Serial serial = null;
private Service service = null;
private volatile boolean connected = false;
private Thread updateThread;

public GUI() {
    initComponents();
    init_serial();
    service = new Service();
    updateThread = new Thread() {
        public void run() {
            while (connected) {
                updateJob();
            }
        }
    };
    updateThread.start();
}

private void init_serial() {
    serial = new Serial();
    serial.searchForPorts();
    serial.connect();
    serial.initIOStream();
    serial.initListener();
}

private void updateJob() {
    ActionListener actListner = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent event) {
            updateState();
        }

    };
    Timer timer = new Timer(5000, actListner);
    timer.start();
}

private void updateState() {
    String portState = service.get_port_state();
    serial.write(portState);
    System.out.println(portState);
}


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    connected = true;
    logger.setText(null);
    logger.setText("connected");
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    logger.setText(null);
    logger.setText("disconnected");
}                                        


public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new GUI().setVisible(true);

        }
    });
}

}

but it didn't work as expected, my question is how can i fix my code and how to put the thread correctly?


Solution

  • You can use a Thread object in class's member and you can start and stop in button click action events. Here is the sample to start/stop thread.

    public class GUI extends javax.swing.JFrame {
        Thread updateThread = null;
        public GUI() {
            JButton btnStart = new JButton("Start");
            JButton btnStop = new JButton("Stop");
            JPanel jPanel = new JPanel();
            jPanel.setBounds(0, 0, 100, 200);
            jPanel.add(btnStart);
            jPanel.add(btnStop);
            add(jPanel);
            btnStart.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    updateThread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            while (true) {
                                System.out.println("Work updated");
                                try {
                                    Thread.sleep(1000);//Time to wait for next routine
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
                    updateThread.start();
                }
            });
            btnStop.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    updateThread.stop();
    
                }
            });
            setVisible(true);
            setBounds(0, 0, 100, 200);
        }
    
        public static void main(String[] args) {
            new GUI();
        }
    
    }