Showing posts with label JOptionPane. Show all posts
Showing posts with label JOptionPane. Show all posts

Sunday, April 8, 2012

Creating a simple Progress Bar using JProgressBar

Program Description:

Today, I have created a simple progress bar program in Java using JProgressBar which consists of two separate programs/classes, the Main Program and the Plugin which is the progress bar property. The reason I separate these two classes is to understand the flow of the progress bar property easily which is our main objective in this project. So enjoy and leave some comments.

Output:
Code:

simpleProgressBar.java
/**
 * File: simpleProgressBar.java
 * Tiltle: Creating a Simple Progress Bar
 * Author: http://java-program-sample.blogspot.com/
 */

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
import java.awt.event.*;

public class simpleProgressBar extends JFrame {
 
 //Constructing JProgressBar and JButton 
 JProgressBar bar = new JProgressBar();
 JButton button = new JButton("Test Progress Bar");
    
 //Setting up GUI
    public simpleProgressBar() {
          
     //Setting up the Title of the Window
     super("Creating a Simple Progress Bar");

     //Set Size of the Window (WIDTH, HEIGHT)
     setSize(350,100);

     //Exit Property of the Window
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     //Setting up the container ready for the components to be added.
     Container pane = getContentPane();
     setContentPane(pane);
     
     //Display the progress bar completion percentage label
     bar.setStringPainted(true);

     //Setting up the container layout
     GridLayout grid = new GridLayout(2,1);
     pane.setLayout(grid);
     
     //Adding the progress bar and the button to the container
     pane.add(bar);
     pane.add(button);
     
     //Implemeting Even-Listener on JButton
  button.addActionListener(
  new ActionListener() {
   
   //Handle JButton event if it is clicked
   public void actionPerformed(ActionEvent event) {
    button.setEnabled(false); //Disable the JButton if the progress bar starts the progress
    Thread run = new threadPlugin(bar); //Calling the class "threadPlugin" we created that extends with Thread
    run.start(); //run the thread to start the progress
    }
   }
  );

     /**Set all the Components Visible.
      * If it is set to "false", the components in the container will not be visible.
      */
     setVisible(true);
    }
    
 //Main Method
    public static void main (String[] args) {
     simpleProgressBar spb = new simpleProgressBar();
 }
}

threadPlugin.java
/**
 * File: threadPlugin.java
 * Tiltle: Creating a Simple Progress Bar (PLUGIN)
 * Author: http://java-program-sample.blogspot.com/
 */
 
 //Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
 
public class threadPlugin extends Thread {

  int Delay = 100; //Creating a delay or the speed of the progress bar
  JProgressBar pb; //Constructing Progress Bar
  
  //Creating a threadPlugin Method initializing JProgressBar so the Main Program "simpleProgressBar.java"
  //can recognize by the time we call this class for JProgressBar action.
  public  threadPlugin(JProgressBar progressbar) {
   pb = progressbar;
  }
  
  //run Method. This is the area where we can adjust the performance of our progress bar.
  public void run() {
      int minimum = pb.getMinimum(); //initializing minimum value of the progress bar
      int maximum = pb.getMaximum(); //initializing maximum value of the progress bar
      
      //Initializing Progress from its minimum value 0 to its maximum value 100
      for (int i = minimum; i < maximum; i++) { 
        try {
          int value = pb.getValue();
          pb.setValue(value + 1);
          
          //Testing the progress bar if it already reaches to its maximum value
          if (pb.getValue() >= maximum) {
          
          //Test confirmation if it runs perfectly
           JOptionPane.showMessageDialog(null, "Test Successful!","Success!",JOptionPane.INFORMATION_MESSAGE);
          }
           
          Thread.sleep(Delay); //Implementing the speed of the progress bar
        } catch (InterruptedException ignoredException) { //Catch an error if there is any
        }
      }
    }
 }

Saturday, August 27, 2011

Arithmetic Operation using JOptionPane

Program Description:

The program below is a simple java code arithmetic operation using JButton as arithmetic operator selector and JOptionPane as user input. It is also capable of catching number format error like putting non-integer numbers.

Output:



Code:

/**
 * File: arithmeticOperationJOptionPane.java
 * Tiltle: Arithmetic Operation using JOptionPane
 * Author: http://java-program-sample.blogspot.com/
 */

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
import java.awt.event.*;

public class arithmeticOperationJOptionPane extends JFrame {
	
	//Initializing JButton and String operation as the label of each JButton
	private JButton buttons[];
	private String operation[] = {"Addition [+]","Subtraction [-]","Multiplication [x]","Division [/]"};

	//Setting up GUI
    public arithmeticOperationJOptionPane() {
    	
    	//Setting up the Title of the Window
    	super("Arithmetic Operation using JOptionPane");

    	//Set Size of the Window (WIDTH, HEIGHT)
    	setSize(320,120);

    	//Exit Property of the Window
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	//Constructing JButton with a array size of 4
    	buttons = new JButton[4];
    	
    	//Setting up the container ready for the components to be added.
    	Container pane = getContentPane();
    	setContentPane(pane);

    	//Setting up the container layout
    	GridLayout grid = new GridLayout(2,2);
    	pane.setLayout(grid);
    	
    	//Constructing all 4 JButtons using "for loop" and add them in the container
    	for(int count=0; count<buttons.length; count++) {
    		buttons[count] = new JButton(operation[count]);
    		pane.add(buttons[count]);
    	}
		
		//Implemeting Even-Listener on JButton button[0] which is addition
		buttons[0].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try { //fetch an error using "try-catch" function.
				
				//Initializing important variables for operation
				String input1, input2;
				int num1, num2, result;
				
				//Making two JOptionPane inputs
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				//Converting the inputs to integer in order to do the Arithmetic operation by parsing the inputs.
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				//Processing Arithmetic Operation
				result = num1 + num2;
				
				//Display the result using JOptionPane
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){ //Catch the error if the user inputs a non-integer value
				
						//Display the error
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		);
		
		//Implemeting Even-Listener on JButton button[1] which is subtraction
		buttons[1].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try {
				String input1, input2;
				int num1, num2, result;
				
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				result = num1 - num2;
				
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);

				}
			}
		}
		);
		
		//Implemeting Even-Listener on JButton button[2] which is multiplication
		buttons[2].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try {
				String input1, input2;
				int num1, num2, result;
				
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				result = num1 * num2;
				
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);

				}
			}
		}
		);
		
		//Implemeting Even-Listener on JButton button[3] which is division
		buttons[3].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try {
				String input1, input2;
				int num1, num2, result;
				
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				result = num1 / num2;
				
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);

				}
			}
		}
		);
		
		buttons[0].setMnemonic('A');
		buttons[1].setMnemonic('S');
		buttons[2].setMnemonic('M');
		buttons[3].setMnemonic('D');
		
    	/**Set all the Components Visible.
    	 * If it is set to "false", the components in the container will not be visible.
    	 */
    	setVisible(true);
    	setResizable(false); //Fix window height and width
    }
    
	//Main Method
    public static void main (String[] args) {
    	arithmeticOperationJOptionPane pjtf = new arithmeticOperationJOptionPane();
	}
}

Important Part of the Program:

//Constructing all 4 JButtons using "for loop" and add them in the container
    	for(int count=0; count<buttons.length; count++) {
    		buttons[count] = new JButton(operation[count]);
    		pane.add(buttons[count]);
    	}
		
		//Implemeting Even-Listener on JButton button[0] which is addition
		buttons[0].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try { //fetch an error using "try-catch" function.
				
				//Initializing important variables for operation
				String input1, input2;
				int num1, num2, result;
				
				//Making two JOptionPane inputs
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				//Converting the inputs to integer in order to do the Arithmetic operation by parsing the inputs.
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				//Processing Arithmetic Operation
				result = num1 + num2;
				
				//Display the result using JOptionPane
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){ //Catch the error if the user inputs a non-integer value
				
						//Display the error
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		);
		
		//Implemeting Even-Listener on JButton button[1] which is subtraction
		buttons[1].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try {
				String input1, input2;
				int num1, num2, result;
				
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				result = num1 - num2;
				
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);

				}
			}
		}
		);
		
		//Implemeting Even-Listener on JButton button[2] which is multiplication
		buttons[2].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try {
				String input1, input2;
				int num1, num2, result;
				
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				result = num1 * num2;
				
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);

				}
			}
		}
		);
		
		//Implemeting Even-Listener on JButton button[3] which is division
		buttons[3].addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				try {
				String input1, input2;
				int num1, num2, result;
				
				input1 = JOptionPane.showInputDialog("Please Input First Number: ");
				input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
				
				num1 = Integer.parseInt(input1);
				num2 = Integer.parseInt(input2);
				
				result = num1 / num2;
				
				JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
				} catch (NumberFormatException e){
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);

				}
			}
		}
		);

Thursday, August 18, 2011

Basic Arithmetic Operation using JRadioButton, JTextField, and JButton

Program Description:

The program below is a java code, a progject that demonstrates basic arithmetic operations using JTextField, JRadioButton, and JButton. This is a revised code from the blog PlaneCodes which I try to make the codes more understandable by putting comments and making it more organized.

The program can detect if there is no arithmetic operator selected. The program only accepts integer numbers and it will detect if the user puts double or float or string. The program can also recognize if the JTextFields are empty.

Output:
Code:

/**
 * File: basicArithmeticOperation.java
 * Tiltle: Basic Arithmetic Operation using JRadioButton, JTextField, and JButton
 * Author: http://java-program-sample.blogspot.com
 */

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
import java.awt.event.*;

public class basicArithmeticOperation extends JFrame {
	
	//Initializing JButton, JPanel, JRadioButton, JTextField, and the class ButtonGroup
	private JPanel panel1, panel2;
	private JRadioButton addition, multiplication, subtraction, division;
	private JTextField input1, input2;
	private JButton answer, reset;
	private ButtonGroup group;
	

	//Setting up GUI
    public basicArithmeticOperation() {
    	
    	//Setting up the Title of the Window
    	super("Basic Arithmetic Operation: +,-,*,/");

    	//Set Size of the Window (WIDTH, LENGTH)
    	setSize(475,100);

    	//Exit Property of the Window
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	//Constructing JRadioButton
    	addition = new JRadioButton("Addition[+]",false);
    	multiplication = new JRadioButton("Multiplication[x]",false);
    	subtraction = new JRadioButton("Subtraction[-]",false);
    	division = new JRadioButton("Division[/]",false);
		
		//Constructing JPanel 1
		panel1 = new JPanel();
		panel1.setLayout(new GridLayout(1,4)); //setting layout on JPanel 1
		
		//Adding JRadioButton in JPanel
		panel1.add(addition);
		panel1.add(multiplication);
		panel1.add(subtraction);
		panel1.add(division);
		
		//Constructing the class ButtonGroup
		group = new ButtonGroup();
		
		//Group the four radio button using the class ButtonGroup
		group.add(addition);
		group.add(multiplication);
		group.add(subtraction);
		group.add(division);		
		
		//Constructing JPanel 2
		panel2 = new JPanel();
    	panel2.setLayout(new GridLayout(1,4)); //setting layout on JPanel 2
    	
    	//Constructing JTextField input1 and input2 with a size of 20
    	input1 = new JTextField(20);
    	input2 = new JTextField(20);
    	
    	//Constructing JButton
    	answer = new JButton("Show Answer");
    	reset = new JButton("Reset");
    	
    	//Adding JButton and JTextField in JPanel 2
    	panel2.add(input1);
    	panel2.add(input2);
    	panel2.add(answer);
    	panel2.add(reset);

    	//Setting up the container ready for the components to be added.
    	Container pane = getContentPane();
    	setContentPane(pane);
    	
    	GridLayout grid = new GridLayout(2,1);
    	pane.setLayout(grid);
    	
    	//Implemeting Even-Listener on JButton copy
		answer.addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				
				//If no arithmetic operator selected, a pop-up message will appear.
				if (addition.isSelected() == false && multiplication.isSelected() == false && subtraction.isSelected() == false && division.isSelected() == false) {
						JOptionPane.showMessageDialog(null, "Please Select an Arithmetic Operator","Error",JOptionPane.ERROR_MESSAGE);
				}
				
				if(addition.isSelected()==true){	//if "addition" radio button is selected
					try {	//fetch an error using "try-catch" function.
					int a = Integer.parseInt(input1.getText()); //Convert JTextField input1 to integer by parsing and pass it to a variable "a" ready for arithmetic processing.
					int b = Integer.parseInt(input2.getText()); //Convert JTextField input2 to integer by parsing and pass it to a variable "b" ready for arithmetic processing.
					int ans = a+b; //Process the two inputs using addition.
					JOptionPane.showMessageDialog(null, "The Answer is: "+ans,"Answer:",JOptionPane.INFORMATION_MESSAGE); //Display the answer using JOptionPane
					} catch (NumberFormatException e){ //Catch the error if the user inputs a non-integer or number
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE); //Display the catched error using JOptionPane
					} //end of try-catch function
				} //end of "if" condition
				
				if(multiplication.isSelected()==true){
					try {
					int a = Integer.parseInt(input1.getText());
					int b = Integer.parseInt(input2.getText());
					int ans = a*b;
					JOptionPane.showMessageDialog(null, "The Answer is: "+ans,"Answer:",JOptionPane.INFORMATION_MESSAGE);
					} catch (NumberFormatException e){
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
					}

				}
				
				if(subtraction.isSelected()==true){
					try {
					int a = Integer.parseInt(input1.getText());
					int b = Integer.parseInt(input2.getText());
					int ans = a-b;
					JOptionPane.showMessageDialog(null, "The Answer is: "+ans,"Answer:",JOptionPane.INFORMATION_MESSAGE);
					} catch (NumberFormatException e){
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
					}

				}
				
				if(division.isSelected()==true){
					try {
					int a = Integer.parseInt(input1.getText());
					int b = Integer.parseInt(input2.getText());
					int ans = a/b;
					JOptionPane.showMessageDialog(null, "The Answer is: "+ans,"Answer:",JOptionPane.INFORMATION_MESSAGE);
					} catch (NumberFormatException e){
						JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		}
		);
		
		//Implemeting Even-Listener on JButton reset
		reset.addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				
				input1.setText(null); //Empty JTextField input1 ready for the next input
				input2.setText(null); //Empty JTextField input2 ready for the next input
			}
		}
		);
		
		//Adding the two panels in the container
    	pane.add(panel1);
    	pane.add(panel2);

    	/**Set all the Components Visible.
    	 * If it is set to "false", the components in the container will not be visible.
    	 */
    	setVisible(true);
    	setResizable(false);
    }
    
	//Main Method
    public static void main (String[] args) {
    	basicArithmeticOperation bao = new basicArithmeticOperation();
	}
}

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hostgator Discount Code