title: Calling a Background Thread in Swing
body: <p>First code:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  public class cos {
     public static int a;
  
     private static JLabel labeler;
  
     // public static Runnable r1;
  
     private JFrame frame;
  
     /**
      * Launch the application.
      */
     public static void main(String[] args) {
        a = 0;
        EventQueue.invokeLater(new Runnable() {
           public void run() {
              try {
                 cos window = new cos();
                 window.frame.setVisible(true);
              } catch (Exception e) {
                 e.printStackTrace();
              }
           }
        });
  
     }
  
     /**
      * Create the application.
      */
     public cos() {
        initialize();
     }
  
     /**
      * Initialize the contents of the frame.
      */
     public void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 205, 194);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
        JLabel lblTime = new JLabel("Time:");
        frame.getContentPane().add(lblTime, BorderLayout.WEST);
  
        final JLabel labeler = new JLabel("");
        frame.getContentPane().add(labeler, BorderLayout.CENTER);
  
        JButton btnNewButton = new JButton("New button");
        btnNewButton.addActionListener(new ActionListener() {
           Runnable r1 = new Runnable() {
              public void run() {
                 while (a &lt;= 10) {
                    a = a + 1;
                    labeler.setText(Integer.toString(a));
                    try {
                       Thread.sleep(1000);
                    } catch (InterruptedException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                    }
                 }
  
              }
           };
  
           public void actionPerformed(ActionEvent arg0) {
              Thread threder = new Thread(r1);
              threder.start();
              // liczniczek bla = new liczniczek();
  
           }
        });
        frame.getContentPane().add(btnNewButton, BorderLayout.SOUTH);
  
     }
  
     public void licznik() {
        while (a &lt; 60) {
           a = a + 1;
           labeler.setText(Integer.toString(a));
           try {
              Thread.sleep(1000);
           } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
           }
        }
     }
  }
  </code></pre>
  
  <p>And now my question. I wanna use code like this:</p>
  
  <pre><code>             Runnable r1 = new Runnable(){
                  public void run(){
                             licznik();
  
                  }
              };
  </code></pre>
  
  <p>But that doesen't work. What i must do to separate this code ? Sorry for my bad english</p>
  
  <p>Sierran.</p>