Skip to content
Advertisement

Java Thread works on windows but slow and glitches on mac

My code attached below works perfectly fine on a windows system. However on my mac it just doesnt work. It starts on the login Screen and the whole thing just slows and glitches out, im unable to select the button or to exit the frame all functions that i can do on my windows system any suggested fix? I belive it is the threading, again the code works perfectly fine on a windows counter part its just on mac.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class Screen extends JFrame{​​​​

        Screen(String sc){​​​​
                setTitle(sc);
                setSize(1000,600);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setVisible(false);
        }​​​​
        public void isVisible(boolean val){​​​​
                setVisible(val);
        }​​​​

}​​​​

public class Index extends Thread{

        static class LoginScreen extends Screen implements ActionListener{
                public LoginScreen(String a){
                        super(a);
                        JButton b = new JButton("Switch");
                        b.addActionListener(this);
                        add(b);   
                }

                public void actionPerformed(ActionEvent e){
                        login = false;
                        home = true;
                }

        }

        static class HomeScreen extends Screen implements ActionListener{
                public HomeScreen(String a){
                        super(a);
                        JButton b = new JButton("Switch");
                        b.addActionListener(this);
                        add(b);    
                }       
                
                public void actionPerformed(ActionEvent e){
                        home = false;
                        login = true;
                }

        }
        static boolean login = true;
        static boolean home = false;

        static LoginScreen ls = new LoginScreen("LoginScreen");
        static HomeScreen hs = new HomeScreen("HomeScreen"); 

        public void screenSetState(HomeScreen hs, LoginScreen ls){
                for(;;){
                        if(login){
                                hs.isVisible(false);
                                ls.isVisible(true);
                        }else if(home){
                                ls.isVisible(false);
                                hs.isVisible(true);
                        }
                }
        }

        public static void main(String ar[]){
                Index mainProg = new Index();
                mainProg.start(); 
        }

        public void run(){
                screenSetState(hs,ls);
        }

}

Above is the attached code for my file.

Advertisement

Answer

Problem is what you’re doing in the thread.

for (ever)
 if(login){
    hs.isVisible(false);
    ls.isVisible(true);
 }else if(home){
    ls.isVisible(false);
    hs.isVisible(true);
 }

This is continuously toggling your login screen and home screen.

Get rid of the loop, you don’t need it. Put the toggles where you want the event to occur.

public void actionPerformed(ActionEvent e){
    super.isVisible(false);
    ls.isVisible(true);
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement