Search code examples
methodsnetbeansjdialog

How to stop a method from running and run this method instead


It is a jdialouge project on netbeans and the methods are program of an 3 analog clock with assigned themes named classic, candy, cyberpunk, and there are buttons that will call each method where the aim is to visualize a change of theme while still running the clock, but when I click the button, both methods are like both running together and what is happening is, its blinking showing the designs of classic and candy each time but the clock still functions


public class NewJDialog extends javax.swing.JDialog {
    
    int xMouse, yMouse;
    /**
    * classic look
    */
    public static void classic(){
        new Thread(){
            @Override
            public void run(){
                while(true){
                    Calendar Cal= new GregorianCalendar();
                    int sec= Cal.get(Cal.SECOND);
                    int min= Cal.get(Cal.MINUTE);
                    int hour= Cal.get(Cal.HOUR);                                      
                    /**
                    * Seconds (sec)
                    */
                    ImageIcon img1 = new ImageIcon(getClass().getClassLoader().getResource("Assets\\DefaultTheme\\SecondsHand\\SC"+sec+".png"));
                    jRotateHand.setIcon(img1);
                    /**
                    * Minute (min)
                    */
                    ImageIcon img2 = new ImageIcon(getClass().getClassLoader().getResource("Assets\\DefaultTheme\\MinuteHand\\MC"+min+".png"));
                    jLonghand.setIcon(img2);
                    /**
                    * Hour (hour)
                    */
                    ImageIcon img3 = new ImageIcon(getClass().getClassLoader().getResource("Assets\\DefaultTheme\\HourHand\\HC"+(hour*5+min*5/60)+".png"));
                    jShorthand.setIcon(img3);
                    
                    ImageIcon b1 = new ImageIcon(getClass().getClassLoader().getResource("Assets\\DefaultTheme\\body.png"));
                    jBody.setIcon(b1);
                    ImageIcon f1 = new ImageIcon(getClass().getClassLoader().getResource("Assets\\DefaultTheme\\shield.png"));
                    jFace.setIcon(f1);
                    ImageIcon p1 = new ImageIcon(getClass().getClassLoader().getResource("Assets\\DefaultTheme\\point.png"));
                    jPoint.setIcon(p1);                    
                    try {
                        sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(NewJDialog.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }.start();
    }
    /**
    * Candy look
    */
    public static void candy(){
        new Thread(){
            @Override
            public void run(){
                while(true){
                    Calendar Cal= new GregorianCalendar();
                    int sec2= Cal.get(Cal.SECOND);
                    int min2= Cal.get(Cal.MINUTE);
                    int hour2= Cal.get(Cal.HOUR);                                      
                    /**
                    * Seconds (sec)
                    */
                    ImageIcon img1 = new ImageIcon(getClass().getClassLoader().getResource("Assets\\CandyTheme\\SecondsHands\\CH"+sec2+".png"));
                    jRotateHand.setIcon(img1);
                    /**
                    * Minute (min)
                    */
                    ImageIcon img2 = new ImageIcon(getClass().getClassLoader().getResource("Assets\\CandyTheme\\HourHand\\HCC"+min2+".png"));
                    jLonghand.setIcon(img2);
                    /**
                    * Hour (hour)
                    */
                    ImageIcon img3 = new ImageIcon(getClass().getClassLoader().getResource("Assets\\CandyTheme\\MinuteHand\\MCC"+(hour2*5+min2*5/60)+".png"));
                    jShorthand.setIcon(img3);
                    
                    ImageIcon b2 = new ImageIcon(getClass().getClassLoader().getResource("Assets\\CandyTheme\\Candybody.png"));
                    jBody.setIcon(b2);
                    ImageIcon f2 = new ImageIcon(getClass().getClassLoader().getResource("Assets\\CandyTheme\\Cshield.png"));
                    jFace.setIcon(f2);
                    ImageIcon p2 = new ImageIcon(getClass().getClassLoader().getResource("Assets\\CandyTheme\\Candypoint.png"));
                    jPoint.setIcon(p2);
                                        
                    try {
                        sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(NewJDialog.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }.start();
    }
    /**
    * Cyberpunk look
    */
    public static void clock3(){
        new Thread(){
            @Override
            public void run(){
                while(true){                   
                    Calendar Cal= new GregorianCalendar();
                    int sec= Cal.get(Cal.SECOND);
                    int min= Cal.get(Cal.MINUTE);
                    int hour= Cal.get(Cal.HOUR);                                      
                    /**
                    * Seconds (sec)
                    */
                    ImageIcon img1 = new ImageIcon(getClass().getClassLoader().getResource("Assets\\DefaultTheme\\SecondsHand\\SC"+sec+".png"));
                    jRotateHand.setIcon(img1);
                    /**
                    * Minute (min)
                    */
                    ImageIcon img2 = new ImageIcon(getClass().getClassLoader().getResource("Assets\\BigHand\\B"+min+".png"));
                    jLonghand.setIcon(img2);
                    /**
                    * Hour (hour)
                    */
                    ImageIcon img3 = new ImageIcon(getClass().getClassLoader().getResource("Assets\\LittleHand\\L"+(hour*5+min*5/60)+".png"));
                    jShorthand.setIcon(img3);
                                        
                    try {
                        sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(NewJDialog.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }.start();
    }
    
    public NewJDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        classic();
        
    }
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        candy();
        
    }



The classic() runs first on running the program

In the jButton1ActionPerformed I thought of something like: candy(); classic()stop;

or in the while(true){ classic()stop;

or there are better ways to implement the idea and I am wrong on making it this way


Solution

  • In your current implementation, both the classic() and candy() methods are running in separate threads continuously. When you click the button, it calls the candy() method, but the classic() method keeps running in the background. That's why you see the blinking effect between the classic and candy themes.

    If you want to switch the themes and stop the previous theme from running, you need to introduce a mechanism to stop the previous thread before starting the new one. One way to achieve this is by using a flag to control the execution of the clock code inside the thread.

    Here's an updated version of your code that incorporates this approach:

    public class NewJDialog extends javax.swing.JDialog {
        private volatile boolean running;
    
        /**
         * classic look
         */
        public void classic() {
            running = true;
    
            new Thread(() -> {
                while (running) {
                    // Your clock code for the classic theme
    
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(NewJDialog.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }).start();
        }
    
        /**
         * Candy look
         */
        public void candy() {
            running = true;
    
            new Thread(() -> {
                while (running) {
                    // Your clock code for the candy theme
    
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(NewJDialog.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }).start();
        }
    
        /**
         * Cyberpunk look
         */
        public void clock3() {
            running = true;
    
            new Thread(() -> {
                while (running) {
                    // Your clock code for the cyberpunk theme
    
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(NewJDialog.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }).start();
        }
    
        public NewJDialog(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();
            classic();
        }
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            running = false; // Stop the previous theme
    
            candy(); // Start the candy theme
        }
    
        // Other code...
    }
    

    In this updated code, I've added a running boolean flag that controls the execution of the clock code inside the threads. When you click the button, it sets running to false, which stops the previous theme's thread. Then it calls the candy() method, which starts the new theme's thread.