Monday, February 18, 2013

A Simple Java Multithreading Example


The runnable Java code below demonstrates a simple java multithreading example in a JFrame. You can configure it on your own with the following steps:

1. Copy the "setSimpleThread()" method.

2. Paste it in your JFrame class and implement the "run()" method inside the thread.

3. Add a line "setSimpleThread();" at some position in the constructor.




import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;

public class SimpleMultiThread extends JFrame {

 private static final long serialVersionUID = 1L;
 private JPanel contentPane;
 private JTextField textField;

 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     SimpleMultiThread frame = new SimpleMultiThread();
     frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

 private void setSimpleThread(){
  JButton btnStart = new JButton("Start");
  btnStart.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) {
    final Thread thread = new Thread(new Runnable() {
     @Override
     public void run() {
      textField.setVisible(true);
      try {
       for (int i = 0 ; i < 5 ; i ++){
        textField.setText((5 - i) + "");
        Thread.sleep(1000);
       }
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
      textField.setVisible(false);
     }
    });
    thread.start();
   }
  });
  btnStart.setFont(new Font("Mangal", Font.BOLD, 16));
  btnStart.setBounds(180, 166, 78, 23);
  contentPane.add(btnStart);
 }
 
 /**
  * Create the frame.
  */
 public SimpleMultiThread() {
  setTitle("Simple MultiThread");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(100, 100, 450, 300);
  setResizable(false);
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  contentPane.setLayout(null);
  setContentPane(contentPane);
  
  textField = new JTextField();
  textField.setVisible(false);
  textField.setBorder(new LineBorder(new Color(139, 0, 139), 2));
  textField.setFont(new Font("Tunga", Font.BOLD, 36));
  textField.setBounds(138, 83, 169, 53);
  contentPane.add(textField);
  textField.setColumns(10);
  
  JLabel lblNewLabel = new JLabel("Click Start to Demonstrate Simple Multithreading in Java");
  lblNewLabel.setForeground(new Color(0, 0, 255));
  lblNewLabel.setFont(new Font("Batang", Font.PLAIN, 14));
  lblNewLabel.setBounds(45, 32, 368, 15);
  contentPane.add(lblNewLabel);
  
  setSimpleThread(); //display a simple multithreading example
 }
}



No comments:

Post a Comment