Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Swing & Threading ... Systen hangs

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Swing & Threading ... Systen hangs

    Consider the code below ...


    public class frmMain extends javax.swing.JFrame {


    Boolean running;


    public class MyThread extends Thread
    {
    public frmMain owner;
    public int value = 0;

    public void run()
    {

    value = 0;

    while (value < 200000)
    {
    if (running == false) break;

    owner.setTitle(String.valueOf(value));


    value++;

    }



    }
    }


    /**
    * Creates new form frmMain
    */
    public frmMain() {
    initComponents();
    }



    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:

    running = true;

    MyThread j = new MyThread();
    j.owner = this;
    j.start();


    }

    private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:


    running = false;


    }

    -----------------------------------

    Basically I have a long process in my Swing code. Within that process I want to make sure my jFrame & graphics is constantly being updated & refreshed. My process is in a background thread, but I need a way to ensure that the thread is detached from the jFrame and that the jFrame can display what is happening in the process.

    This is a threading issue I believe.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Swing & Threading ... Systen hangs

    Can you make a program that compiles, executes and shows the problem?
    What happens when the posted code executes?

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Swing & Threading ... Systen hangs

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    /** Resolve the unresponsive UI problem by running the compute-intensive task
        in this own thread, which yields control to the EDT regularly */
    public class UnresponsiveUIwThreadSleep extends JFrame {
       private boolean stop = false;
       private JTextField tfCount;
       private int count = 1;
     
       /** Constructor to setup the GUI components */
       public UnresponsiveUIwThreadSleep() {
          Container cp = getContentPane();
          cp.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
          cp.add(new JLabel("Counter"));
          tfCount = new JTextField(count + "", 10);
          tfCount.setEditable(false);
          cp.add(tfCount);
     
          JButton btnStart = new JButton("Start Counting");
          cp.add(btnStart);
          btnStart.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent evt) {
                stop = false;
                // Create a new Thread to do the counting
                Thread t = new Thread() {
                   @Override
                   public void run() {  // override the run() for the running behaviors
                      for (int i = 0; i < 100000; i++) {
                         if (stop) break;
                         tfCount.setText(count + "");
                         count++;
                         // Suspend this thread via sleep() and yield control to other threads.
                         // Also provide the necessary delay.
                         try {
                            sleep(10);  // milliseconds
                         } catch (InterruptedException ex) {}
                      }
                   }
                };
                t.start();  // call back run()
             }
          });
     
          JButton btnStop = new JButton("Stop Counting");
          cp.add(btnStop);
          btnStop.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent evt) {
                stop = true;
             }
          });
     
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setTitle("Counter");
          setSize(300, 120);
          setVisible(true);
       }
     
       /** The entry main method */
       public static void main(String[] args) {
          // Run GUI codes in Event-Dispatching thread for thread safety
          javax.swing.SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                new UnresponsiveUIwThreadSleep();  // Let the constructor do the job
             }
          });
       }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Swing & Threading ... Systen hangs

    Can you explain what executing the posted code is supposed to show?
    On starting execution: it shows two buttons, after pressing Start the count field changes. After pressing Stop, the count field stops changing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Swing & Threading ... Systen hangs

    that code was an example of how to handle threads in Swing such that ur GUI doesn't become unresponsive.
    Do not create swing gui in main thread.
    Do not make a function call do a lot of work on button click event.Instead spawn a new thread so that ur EDT is responsive and gui refreshes

Similar Threads

  1. Threading In java
    By pranav.bpatil in forum Threads
    Replies: 2
    Last Post: February 7th, 2012, 01:23 AM
  2. BufferedReader hangs when handling POST method data
    By Sucker Punch in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2012, 04:33 PM
  3. Replies: 2
    Last Post: January 6th, 2012, 10:50 PM
  4. Network communication hangs
    By droyhull in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 8th, 2011, 10:55 AM
  5. Thread Hangs at split method.
    By kailasvilaskore in forum Threads
    Replies: 1
    Last Post: November 26th, 2010, 12:13 PM