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


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


public class invokelater3 extends JFrame  {


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

  public invokelater3() {
    //setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }


  //Überschreiben, damit das Programm bei Herunterfahren des Systems beendet werden kann
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }


  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() );

    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(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() {
     myDialog frame = new myDialog();
     frame.setVisible(true);

     myThread thread1 = new myThread(frame.bar1, frame.label1);
     thread1.start();

     myThread thread2 = new myThread(frame.bar2, frame.label2);
     thread2.start();
   }


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

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

}  // invokelater1


class myDialog extends JFrame  {


  public JProgressBar bar1 = new JProgressBar();
  public JProgressBar bar2 = new JProgressBar();
  public JLabel label1 = new JLabel("1");
  public JLabel label2 = new JLabel("2");

  public myDialog() {
    //setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }


  //Überschreiben, damit das Programm bei Herunterfahren des Systems beendet werden kann
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }


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


     bar1.setMaximum(1000);
     bar1.setStringPainted(true);
     bar2.setMaximum(1000);
     bar2.setStringPainted(true);

    label1.setFont( new Font("Arial", Font.BOLD,28)  );
    label2.setFont( new Font("Arial", Font.BOLD,28)  );

    bar1.setFont( new Font("Arial", Font.BOLD,28)  );
    bar2.setFont( new Font("Arial", Font.BOLD,28)  );

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

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

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

    getContentPane().add(bar1,
      new GridBagConstraints(
       0, 2, 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, 3, 1, 1, 1.0, 1.0,     // gridx, gridy, gridwidth, gridheight, weightx, weighty
       GridBagConstraints.WEST,  // anchor
       GridBagConstraints.BOTH,  // HORONZONTAL, VERTICAL BOTH
       new Insets(20,20,40,20), 0, 0)); // Top, left, bottom, right


  } // setGUI


}  // myDialog


class myThread extends Thread {

  JProgressBar bar;
  JLabel label1;

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

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

/*
SwingUtilities.invokeLater(new Runnable() {
            public void run() {
              aufgaben();
            }
          });
*/