

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



//  Beispiel mit Slider und ProgressBar
//   Es wird ein Thread erzeugt, in der eine "berechnung durchgeführt wird



public class GUI_08 extends JFrame {
  
    JLabel label1 = new JLabel("Slider");
    JSlider slider1 =  new JSlider(45,111); // bis jdk 1.2  muss minimalwert <= 50 sein
    
    /*
     public	JSlider()
    public	JSlider(int orientation)
    public	JSlider(int min, int max)
    public	JSlider(int min, int max, int value)
    public	JSlider(int orientation, int min, int max, int value)
     */
 
    JLabel label2 = new JLabel("ProgressBar");
    JProgressBar bar1 =  new JProgressBar();
    
  private JButton BnOk = new JButton("Ok");
  private JButton BnESC = new JButton("Abbruch");
    

  private JButton BnUp = new JButton("Up");
  private JButton BnDown = new JButton("Down");

  private JButton BnStart = new JButton("Start Thread");
  private JButton BnStop= new JButton("Stop Thread");
  
  private myThread  thr=null;  // wichtig

  public GUI_08() {
    setGUI();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    setLocation(200,200);
    setSize(500, 300);
  }

  private void setGUI() {
    setTitle("GUI mit JSlider und JProgressBar");
    this.getContentPane().setLayout( new GridBagLayout() );
    this.getContentPane().add(label1,
           new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE,
            new Insets(30,10,0,10), 0, 0));

    this.getContentPane().add(slider1,
           new GridBagConstraints(1, 0, 1, 1, 100.0, 0.0
            ,GridBagConstraints.NORTHEAST, GridBagConstraints.HORIZONTAL,
            new Insets(30,10,0,10), 0, 0));

    label1.setFont( new Font("Arial", Font.BOLD,18)  );
    slider1.setFont( new Font("Arial", Font.BOLD,18)  );


    // --------------------------------
    
    this.getContentPane().add(label2,
           new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE,
            new Insets(30,10,0,10), 0, 0));

    this.getContentPane().add(bar1,
           new GridBagConstraints(1, 1, 1, 1, 100.0, 0.0
            ,GridBagConstraints.NORTHEAST, GridBagConstraints.HORIZONTAL,
            new Insets(30,10,0,10), 0, 0));

    label2.setFont( new Font("Arial", Font.BOLD,18)  );
    bar1.setFont( new Font("Arial", Font.BOLD,18)  );


// -----------------------------------
    
   slider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                changeState1(evt);
            }
        });

    bar1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                changeState2(evt);
            }
        });

    
    // -----------------------------------
        
        
    // Schalter
    BnDown.setFont( new Font("Arial", Font.BOLD,18)  );
    BnUp.setFont( new Font("Arial", Font.BOLD,18)  );
    BnStart.setFont( new Font("Arial", Font.BOLD,18)  );
    BnStop.setFont( new Font("Arial", Font.BOLD,18)  );
    JPanel pn2 = new JPanel();
    pn2.setLayout( new FlowLayout() );
    pn2.add(BnDown);
    pn2.add(BnUp);
    pn2.add(BnStart);
    pn2.add(BnStop);
//    pn.setBackground(Color.green);
    this.getContentPane().add(pn2,
           new GridBagConstraints(0, 3, 2, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(30,0,0,0), 0, 0));

    BnDown.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnDownUp_Click(e);
      }
    });

    BnUp.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnDownUp_Click(e);
      }
    });
    
    BnStart.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnStart_Click();
      }
    });
    BnStop.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnStop_Click();
      }
    });
    
    // -----------------------------------
        
        
    // Schalter
    BnOk.setFont( new Font("Arial", Font.BOLD,18)  );
    BnESC.setFont( new Font("Arial", Font.BOLD,18)  );
    JPanel pn = new JPanel();
    pn.setLayout( new FlowLayout() );
    pn.add(BnOk);
    pn.add(BnESC);
//    pn.setBackground(Color.green);
    this.getContentPane().add(pn,
           new GridBagConstraints(0, 4, 2, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(30,0,0,0), 0, 0));

    BnOk.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnOk_Click();
      }
    });
    BnESC.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnESC_Click();
      }
    });

    
    
  } // setGUI

  private void BnDownUp_Click(ActionEvent e){
      if (e.getSource() == BnDown)
          bar1.setValue(  bar1.getValue() -1);

      if (e.getSource() == BnUp)
          bar1.setValue(  bar1.getValue() +1);
      
  }

  
  private void  BnStart_Click() {
      System.out.println("Start Thread");
      thr = new myThread(bar1);
      thr.start();
      
  }

  private void  BnStop_Click() {
      System.out.println("Stop Thread");
      thr.interrupt();  // stop den Thread
      thr=null;
  }

  // JSlider ist numerisch
    private void changeState1(javax.swing.event.ChangeEvent evt) {
        Integer I = (Integer) slider1.getValue();
        System.out.println( I.intValue() );
        /*  jdk 1.2   Source Code */
        /*
          int i = slider1.getValue();
          System.out.println( i );
         */
    }

    // ProgressBar
    private void changeState2(javax.swing.event.ChangeEvent evt) {
        int i = bar1.getValue();
        System.out.println( " Progressbar: "+i );
    }

    

private void BnOk_Click() {
    System.exit(0);
}

private void BnESC_Click() {
    System.exit(0);
}

  public static void main(String[] args) {
    GUI_08 frame = new GUI_08();
    frame.setVisible(true);
 }

}




class myThread extends Thread {
    JProgressBar _bar;
    
    public myThread( JProgressBar bar ) {
        _bar = bar;
        // setze ProgressBar auf den Anfang
        _bar.setValue( _bar.getMinimum()  );
    }
    
    public void run() {
        while (   ( _bar.getValue() < _bar.getMaximum() )  && (! isInterrupted() )   ) {
            _bar.setValue(  _bar.getValue() +1);
            //_bar.updateUI();
             try {
                Thread.sleep(300);
              }
              catch (InterruptedException e) {
                  this.interrupt();  // 
                  System.out.println("Ende in der catch-Anweisung");
            }
            
      }  // while
      System.out.println("NACH DER SCHLEIFE");

                  
    }
    
    
}
