Thursday, October 15, 2009

Unique Example of synchronized threads with MouseListener

Ide@: Arindam Mitra



Below source code is an example of you can manage synchronization of a continuous thread of MouseEntered event with a user defined thread of MouseExited event.



MouseListener interface has abstract methods out of which mouseEntered and mouseExited are the two methods. Suppose, in a JWindow, you have a JProgressBar. When you enter your mouse in the JWindow, it will continuously progress the status of the progress bar. However, if you remove the mouse from the window, progress bar will stop progressing and halts the current status.



Below is the source code. Hope you like it.



import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class WindowProgressBarThread extends JFrame implements MouseListener, ActionListener, Runnable {
JLabel label;
JButton buttonClick;
JButton buttonClose;
JWindow window;
JProgressBar jProgressBar = new JProgressBar();
private static int statusInt = 0;
private Thread runner;
// A thread that runs as long as the timer
// is running. It sets the text in the label
// to show the elapsed time. This variable is
// non-null when the timer is running and is
// null when it is not running. (This is
// tested in mousePressed().)

// Constants for use with status variable.
private static final int GO = 0, TERMINATE = 1;

private int status;
// This variable is set by mouseEntered() and mouseExited()
// to control the thread. When it's time
// for the thread to end, the value is
// set to TERMINATE.

public WindowProgressBarThread() {

buttonClick = new JButton(" Click On It ");
buttonClose = new JButton("Close");
jProgressBar.setMaximum(100);
jProgressBar.setMinimum(0);
window = new JWindow();

JPanel panel = new JPanel(new GridLayout(1, 0));
JFrame.setDefaultLookAndFeelDecorated(true);
this.setLayout(new BorderLayout());
this.setDefaultLookAndFeelDecorated(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel.add(buttonClick);

this.add(panel, BorderLayout.LINE_START);



this.setSize(200,75);
this.pack();
this.setVisible(true);
this.setResizable(false);
this.setLocationRelativeTo(null);

window.addMouseListener(this);
buttonClick.addActionListener(this);
buttonClose.addActionListener(this);

}


// Run method executes while the timer is going.
// Several times a second, it computes the number of
// seconds the thread has been running and displays the
// whole number of seconds on the label. It ends
// when status is set to TERMINATE.
public void run() {
while (true) {
synchronized(this) {
if (status == TERMINATE) {
break;
}
if(statusInt <= 99) { statusInt = statusInt + 1; jProgressBar.setValue(statusInt); jProgressBar.setString(statusInt + "%"); jProgressBar.setStringPainted(true); } } waitDelay(100); } } synchronized void waitDelay(int milliseconds) { // Pause for the specified number of milliseconds OR // until the notify() method is called by some other thread. // (From Section 7.5 of the text.) try { wait(milliseconds); } catch (InterruptedException e) { } } public void mousePressed(MouseEvent evt) { } public void mouseReleased(MouseEvent evt) { } public void mouseClicked(MouseEvent evt) { } synchronized public void mouseEntered(MouseEvent evt) { status = GO; runner = new Thread(this); runner.start(); // start the runner thread (can call it child thread) } synchronized public void mouseExited(MouseEvent evt) { status = TERMINATE; notify(); // Wake up the main thread so that child thread can terminate quickly. jProgressBar.setValue(statusInt); jProgressBar.setString(statusInt + "%"); jProgressBar.setStringPainted(true); runner = null; // stop the runner thread (can call it child thread) } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==buttonClick) { buttonClick.setEnabled(false); statusInt = 0; jProgressBar.setValue(statusInt); jProgressBar.setString(statusInt+"%"); jProgressBar.setStringPainted(true); window.add(new JLabel("Progress Bar Demo",SwingConstants.CENTER), BorderLayout.NORTH); window.add(jProgressBar, BorderLayout.CENTER); window.add(buttonClose, BorderLayout.SOUTH); window.pack(); window.setBounds(800, 500, 200, 75); window.setVisible(true); } if(ae.getSource()==buttonClose){ buttonClick.setEnabled(true); window.setVisible(false); } } public static void main(String[] args) { WindowProgressBarThread windoeProgressThread = new WindowProgressBarThread(); } }


1 comment:

kavi said...

hey... it's Gt8..i like it...

Total Pageviews