Jump to content

Prevent button from freezing

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
mr_skyflakes21

mr_skyflakes21

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
We created this program in Java. In a jPanel, we insert a button. Its function is decided to initiate a method beginExecution(); Inside of this method, there is Thread.sleep(700); so yes, when i press the start button, the process starts, but i can interact with the swing!. I know that its because of the Thread.sleep()
which pauses the system. But i dont know how to make the button not freezing and the gui program interactive.

example


import java....


public class Example extends JPanel{

          public Example(){

           //I add the button here;

           //add an actionlistener to that button

           //i added the button to the panel

           label = new JLabel();

           add(label);        

}


         this is the method to be performed when the button is pressed

         public void beginExec(){

             try

                  Thread.sleep();

                  label.setText(It will print whatever happens in the program);

         }

       here is the class ButtonListener


             public class ButtonListener implements ActionListener{

                      public void actionPerfomed(ActionEvent e){

                        if(start ==   e.getSource()){//if the start is pressed;

                               beginExec();

                         }

                     }

             }


              public static void main(String[] args)

                 add the panel to frame;


}



#2
gammaman

gammaman

    Learning Programmer

  • Members
  • PipPipPip
  • 71 posts
I don't really understand your question. Are you saying that you are trying to decompress the button, but can't because of the thread.sleep().

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Yea don't really understand what you want.. what's the thread.sleep(700) doing anyway. 700milliseconds isn't really noticeable anyway.

So you have a frame with inside 1 label + 1 button, if you click the button something must appeir in the label?

IF that's the case it should be something like this:

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

public class test extends JFrame {
    JLabel label;
    JButton button;
    JPanel panel;
    public test(){
    //I add the button here;
    //add an actionlistener to that button
    //i added the button to the panel
    panel = new JPanel();
    panel.setLayout(new BorderLayout());
    label = new JLabel("change me please");
    add(label,BorderLayout.NORTH);
    button = new JButton("change the label");
    add(button, BorderLayout.CENTER);
    setSize(800,600);
    addActions();
    setVisible(true);

    }

    // this is the method to be performed when the button is pressed
     public void beginExec(){
         try{
              Thread.sleep(700);
              label.setText("It will print whatever happens in the program");

     }catch(Exception e){
             System.out.println("gotcha!");
         }
    }
    //here is the class ButtonListener

    public void addActions(){
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.out.println("test");
                beginExec();
            }
        });
    }


    public static void main(String[] args) {
        test tes = new test();

    }

}
code is a bit different but i don't use extends JPanel

oh 700 milliseconds is actually quite noticeable :p looks like the program lags or has to do a very heavy instruction :p

Edited by wim DC, 26 May 2009 - 06:04 AM.
added code