Search code examples
javaeclipsewindowsmultithreadingthread-sleep

Redraw GUI Prior Thread.Sleep


I thought this would be a lot simpler to me, but I am missing the concept. In a nutshell, I made a method to fade away audio, and used Thread.Sleep to have it 'wait' in a for loop. Prior to that method being called, I set several Labels and Text Fields to not be visible, yet they wait until after the thread sleep to change. I attempted to wrap the labels in a do-while loop to force them, and even tried to call redraw after it, but it didn't fix anything. Any advice would be appreciated:

Edited to include ActionPerformed:

            public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == quit)
        {
            System.exit(0);
        }
        else if(e.getSource() == play)
        {
            System.out.println("PLAY");
            quit.setVisible(false);
            quit.setEnabled(false);
            play.setVisible(false);
            play.setEnabled(false);
            GameActivated();
        }
        else if(e.getSource() == submitTeams)
        {
            teamOneName = teamOneTF.getText();
            teamTwoName = teamTwoTF.getText();

            Swinpublic void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == quit)
        {
            System.exit(0);
        }
        else if(e.getSource() == play)
        {
            System.out.println("PLAY");
            quit.setVisible(false);
            quit.setEnabled(false);
            play.setVisible(false);
            play.setEnabled(false);
            GameActivated();
        }
        else if(e.getSource() == submitTeams)
        {
            teamOneName = teamOneTF.getText();
            teamTwoName = teamTwoTF.getText();

            SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                               teamOneLabel.setVisible(false);
                               teamOneTF.setVisible(false);
                               teamTwoLabel.setVisible(false);
                               teamTwoTF.setVisible(false);
                               submitTeams.setVisible(false);
                }
                });

        }
        else
            System.out.println("Error: Button pressed with no known source.");
    }

Below is the inside of the AudioHandler1 that is being called:

            for(int count = 0; count < 71; count++)
        {
            gainControl.setValue((float) count*-1);
            System.out.println("Waiting!" + Math.random());
            try 
                {
                Thread.sleep(50);
                } 
            catch(InterruptedException ex) 
                {
                    Thread.currentThread().interrupt();
                }

        }

Solution

  • You are blocking the EDT here. Use SwingWorker to invokle AudioHandler1

            new SwingWorker<Void,Void>()
            {
            @Override
            protected Void doInBackground() throws Exception 
                {
                AudioHandler1("Theme.wav",false,true);
                return null;
                }
            }.execute();