jeudi 9 juillet 2020

disable a checkbox before thread is sleeping

i try to disable a checkbox before sleeping but after hitting the button, the checkbox wont disable before sleeping. If sleeping is done the checkbox will disable. How can i disable the checkbox first?

public class Class implements ActionListener {

    private JCheckBox checkbox;
    private JButton button;
    
    public void method() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(235, 168);
        
        checkbox = new JCheckBox("checkbox");
        frame.getContentPane().add(checkbox, BorderLayout.WEST);
        
        button = new JButton("button");
        frame.getContentPane().add(button, BorderLayout.CENTER);
        
        button.addActionListener(this);
        
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        checkbox.setEnabled(false);
        
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire