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.
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.
Re: Swing & Threading ... Systen hangs
Code java:
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
}
});
}
}
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.
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