Java design pattern (2) -- Observer Pattern Swing multi-thread event processing progress bar

Source: Internet
Author: User

Applicable environment: when the user changes, other objects are notified, and other objects are changed. For example, for paging programs in swing, When you click the next page on the previous page, you need to re-activate the event and obtain new data from the database.

In Swing event processing, if multithreading is not used, the main interface is blocked, that is, the interface is white, when multithreading is used for event processing, another problem is that if the user is notified about the progress of event processing and whether the processing is complete, it is best to give the user a prompt during the processing process, indicates the progress of processing. This prompt can be expressed by the progress bar information. The following is a simple instance that uses the progress bar to indicate the event progress. In this example, the Observer mode is also used.
Progress window:
Package pro;

Import java. awt. BorderLayout;
Import java. awt. Color;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java. util. Observable;
Import java. util. Observer;

Import javax. swing. JButton;
Import javax. swing. JLabel;
Import javax. swing. JPanel;
Import javax. swing. JProgressBar;

Public class Progress extends javax. swing. JDialog implements ActionListener {
/**
*
*/
Private static final long serialVersionUID = 1L;

Private JProgressBar pro_JPB;

Private JPanel btn_JP;

Private JButton cancel_JB;

Private JLabel show_JL;

NewJFrame jf;

Public Progress (NewJFrame frame ){
Super (frame, true );
This. jf = frame;
InitGUI ();
}

Set se = new set ();

Private void initGUI (){
Try {

Se. addObserver (new setObserver ());
BorderLayout thisLayout = new BorderLayout ();
GetContentPane (). setLayout (thisLayout );
{
Show_JL = new JLabel ();
GetContentPane (). add (show_JL, BorderLayout. NORTH );
Show_JL.setPreferredSize (new java. awt. Dimension (354, 22 ));
}
{
Pro_JPB = new JProgressBar ();
Pro_JPB.setMinimum (0 );
Pro_JPB.setMaximum (100 );
GetContentPane (). add (pro_JPB, BorderLayout. CENTER );
Pro_JPB.setPreferredSize (new java. awt. Dimension (392, 27 ));
}
{
Btn_JP = new JPanel ();
GetContentPane (). add (btn_JP, BorderLayout. SOUTH );
{
Cancel_JB = new JButton ();
Btn_JP.add (cancel_JB );
Cancel_JB.setText ("" u53d6 "u6d88 ");
Cancel_JB.addActionListener (this );
}
}
This. setBackground (Color. DARK_GRAY.brighter ());
This. setUndecorated (true );
This. setSize (392, 90 );
This. setLocationRelativeTo (null );
} Catch (Exception e ){
E. printStackTrace ();
}
}

Public void setMinmum (int I ){
Pro_JPB.setMinimum (I );
}

Public void setMaxmem (int I ){
Pro_JPB.setMaximum (I );
}

Public void setVal (int I ){
Se. setPrice (I );
}

/**
*
* Set Class-> function: the application of the observer mode.
* 2007-4-17
*/
Class set extends Observable {
Private int price;

Public int getPrice (){
Return price;
}

Public void setPrice (int price ){
This. price = price;
SetChanged ();
Yyobservers (new Integer (price ));
}

}

Class setObserver implements Observer {
Public void update (Observable obj, Object arg ){
If (arg instanceof Integer ){
Int I = (Integer) arg). intValue ();
Pro_JPB.setValue (I );
Show_JL.setText (I-pro_JPB.getMinimum () * 100
/(Float) (pro_JPB.getMaximum ()-pro_JPB.getMinimum ())
+ "% ");
}
}

}

Public void actionreceivmed (ActionEvent e ){
Jf. setTh_Flag (false );
This. setVisible (false );
}

}

Main Program:
Package pro;

Import java. awt. BorderLayout;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;

Import javax. swing. JButton;
Import javax. swing. JPanel;
Import javax. swing. WindowConstants;

Public class NewJFrame extends javax. swing. JFrame {
/**
*
*/
Private static final long serialVersionUID = 1L;

Private JPanel cen_JP;

Private JButton jb;

Public static void main (String [] args ){
NewJFrame inst = new NewJFrame ();
Inst. setVisible (true );
}

Public NewJFrame (){
Super ();
InitGUI ();
}

Progress pro;

Private void initGUI (){
Try {
Setdefaclocloseoperation (WindowConstants. DISPOSE_ON_CLOSE );
{
Cen_JP = new JPanel ();
GetContentPane (). add (cen_JP, BorderLayout. CENTER );
{
Jb = new JButton ();
Cen_JP.add (jb );
Jb. setText ("jButton1 ");
JbAction ja = new jbAction ();
Pro = new Progress (this );
Jb. addActionListener (ja );
}
}
Pack ();
SetSize (400,300 );
} Catch (Exception e ){
E. printStackTrace ();
}
}

Boolean th_Flag = true;

Class jbAction implements ActionListener {
Public void actionreceivmed (ActionEvent e ){
Pro. setMaxmem (100 );
Pro. setMinmum (0 );
New Thread (){
Public void run (){
For (int I = 1; I <100; I ++ ){
If (! IsTh_Flag ()){
SetTh_Flag (true );

Break;
}
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
Pro. setVal (I );
System. out. println ("=" + I );
}
}
}. Start ();
Pro. setVal (0 );
Pro. setVisible (true );
}

}

Public boolean isTh_Flag (){
Return th_Flag;
}

Public void setTh_Flag (boolean th_Flag ){
This. th_Flag = th_Flag;
}
}

Author "l12052124"
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.