//Titel:            Dialog-Fenster
//Version:
//Copyright:		Copyright (c) 2010
//Autor:
//Organisation:
//Beschreibung:


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


public class invokelater2 extends JFrame  {


  JButton BnUp = new JButton("Up");
  JButton BnThread = new JButton("Thread");
  JButton BnEsc = new JButton("Abbruch");
  JProgressBar bar1 = new JProgressBar();
  JProgressBar bar2 = new JProgressBar();

  public invokelater2() {
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  private void setGUI() {
    setSize(1000, 230);
    setTitle("JFrame als Dialog");

    BnUp.setFont( new Font("Arial", Font.BOLD,18)  );
    BnThread.setFont( new Font("Arial", Font.BOLD,18)  );
    BnEsc.setFont( new Font("Arial", Font.BOLD,18)  );
    BnUp.setPreferredSize( BnEsc.getPreferredSize() );
    bar1.setFont( new Font("Arial", Font.BOLD,28)  );
    bar2.setFont( new Font("Arial", Font.BOLD,28)  );

    this.getContentPane().setLayout( new GridBagLayout() );

	JPanel pnButton = new JPanel();
	//pnButton.setBackground(Color.red);
	pnButton.setLayout( new FlowLayout() );
	pnButton.add(BnUp);
	pnButton.add(BnThread);
	pnButton.add(BnEsc);


    getContentPane().add(bar1,
      new GridBagConstraints(
       0, 0, 1, 1, 1.0, 1.0,     // gridx, gridy, gridwidth, gridheight, weightx, weighty
       GridBagConstraints.WEST,  // anchor
       GridBagConstraints.BOTH,  // HORIZONTAL, VERTICAL BOTH
       new Insets(20,20,0,20), 0, 0)); // Top, left, bottom, right


    getContentPane().add(bar2,
      new GridBagConstraints(
       0, 1, 1, 1, 1.0, 1.0,     // gridx, gridy, gridwidth, gridheight, weightx, weighty
       GridBagConstraints.WEST,  // anchor
       GridBagConstraints.BOTH,  // HORONZONTAL, VERTICAL BOTH
       new Insets(20,20,0,20), 0, 0)); // Top, left, bottom, right


    getContentPane().add(pnButton,
      new GridBagConstraints(
       0, 2, 1, 1, 1.0, 0.0,     // gridx, gridy, gridwidth, gridheight, weightx, weighty
       GridBagConstraints.WEST,  // anchor
       GridBagConstraints.HORIZONTAL,  // HORONZONTAL, VERTICAL BOTH
       new Insets(0,20,0,0), 0, 0)); // Top, left, bottom, right

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

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


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

  } // setGUI

    private void BnUp_Click() {
       System.out.println("Hier in Ok");
       bar1.setValue( bar1.getValue()+1 );
    }

   private void BnThread_Click() {
     bar1.setMaximum(1000);
     bar1.setStringPainted(true);
     // bar.setIndeterminate(true);  // ohne Maximalwert
     myThread thread1 = new myThread(bar1);
     thread1.start();

     bar2.setMaximum(1000);
     bar2.setStringPainted(true);
     // bar.setIndeterminate(true);  // ohne Maximalwert
     myThread thread2 = new myThread(bar2);
     thread2.start();
   }


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

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

}  // invokelater1

class myThread extends Thread {

  JProgressBar bar;

  public myThread(JProgressBar bar) {
   this.bar = bar;
  }

  public void run() {
    long i=0;
    while (i<1000000000) {
      if ( (i % 10) == 0) {
        bar.setValue( bar.getValue()+1 );
      }
      i++;
      if ( bar.getValue() >= bar.getMaximum() ) {
        bar.setValue(0);
      }
      if (i>=1000000000) i=0;
    }
  }
}