Showing posts with label JRadioButton. Show all posts
Showing posts with label JRadioButton. Show all posts

Thursday, August 18, 2011

Display Image to another window using JRadioButton

Program Description:

The Java Program below is an answer to the question I read from one of the comments in PlanetCodes. The program demonstrates how to display image to another window using JRadioButton. In this problem, I have created two Java programs the "mainInterface.java" where the JRadioButtons are placed and the "displayInterface.java" which is used to display the images.

Output:

Code:

Main Interface

/**
 * File: mainInterface.java
 * Tiltle: Display Image to another window using JRadioButton
 * 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 mainInterface extends JFrame {
	
	//Intializing JPanel, JRadioButton, and the class Icon
	private JRadioButton orc, warrior, mage;
	private JPanel panel;
	private Icon orcsImage, warriorImage, mageImage;
	
	//Calling the class "displayInterface" to link the two java programs
	private displayInterface di;
	
	//Setting up GUI
    public mainInterface() {
    	
    	//Setting up the Title of the Window
    	super("Main Interface");

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

    	//Exit Property of the Window
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	//Constructing the class Icon
    	orcsImage = new ImageIcon("orcs.jpg");
    	warriorImage = new ImageIcon("warrior.jpg");
    	mageImage = new ImageIcon("mage.jpg");
		
		//Constructing JRadioButton
    	orc = new JRadioButton("Orc",false);
    	warrior = new JRadioButton("Warrior",false);
    	mage = new JRadioButton("Mage",false);
    	
    	//Register Events for JRadioButton
    	RadioButtonHandler handler = new RadioButtonHandler();
    	orc.addItemListener(handler);
    	warrior.addItemListener(handler);
    	mage.addItemListener(handler);
    	
    	//Group the Radio Buttons using the class ButtonGroup
    	ButtonGroup group = new ButtonGroup();
    	group.add(orc);
    	group.add(warrior);
    	group.add(mage);
    	
    	//Constructing JPanel
    	panel = new JPanel();
    	panel.setLayout(new GridLayout(3,1)); //Setting layout on JPanel
    	panel.setBorder(BorderFactory.createTitledBorder("Select an Image")); //Create a titled border on JPanel
    	
    	//Adding JRadioButton on JPanel
    	panel.add(orc);
    	panel.add(warrior);
    	panel.add(mage);

    	//Setting up the container ready for the components to be added.
    	Container pane = getContentPane();
    	setContentPane(pane);
		
		//Adding the JPanel in the container
    	pane.add(panel);

    	/**Set all the Components Visible.
    	 * If it is set to "false", the components in the container will not be visible.
    	 */
    	setVisible(true);
    	setResizable(false); //Set the window to a permanent size
    	setLocation(550,200); //Set the window to a specific location (X Axis, Y Axis)
    	
    }
    
	//Main Method
    public static void main (String[] args) {
    	mainInterface mi = new mainInterface();
	}
	
	//Private Inner class to handle radio button event
	public class RadioButtonHandler implements ItemListener {
		
		//handle radio button event
		public void itemStateChanged(ItemEvent event) {
			
			di = new displayInterface(); //Constructing the class "displayInterface" in order to link the two java program
			
			if(event.getSource() == orc) //Checking what radio button is clicked
				if (event.getStateChange() == ItemEvent.SELECTED) //Tracking if the radio button is selected or deselected
				di.image.setIcon(orcsImage); //if it is checked then the image will be displayed from another window
				else
				di.dispose(); //if not then dispose the window where the image is displayed.
				
				/** You have to remember that radio button has two actions, the "SELECTED" and "DESELECTED".
				 *	The reason why I put another "if condition" and the method "dispose()" is to prevent from displaying
				 *	two windows at the same time when you click any of the radio buttons. Without these functions, the radio 
				 *	button will display images everytime it is "selected" or "deselected".
				 */
			
			if(event.getSource() == warrior)
				if (event.getStateChange() == ItemEvent.SELECTED)
				di.image.setIcon(warriorImage);
				else
				di.dispose();
			
			if(event.getSource() == mage)
				if (event.getStateChange() == ItemEvent.SELECTED)
				di.image.setIcon(mageImage);
				else
				di.dispose();
		}
	}
}

Display Interface

/**
 * File: displayInterface.java
 * Tiltle: Display Image to another window using JRadioButton
 * Author: http://java-program-sample.blogspot.com
 */

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

public class displayInterface extends JFrame {
	
	//Initializing JLabel
	JLabel image;

	//Setting up GUI
    public displayInterface() {
    	
    	//Setting up the Title of the Window
    	super("Display Interface");

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

    	//Exit Property of the Window
    	setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    	    	
    	//Constructing JLabel
    	image = new JLabel();

    	//Setting up the container ready for the components to be added.
    	Container pane = getContentPane();
    	setContentPane(pane);
		
		//Setting up the container layout
    	FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
    	pane.setLayout(flow);
    	
    	//Adding JLabel in the container
    	pane.add(image);

    	/**Set all the Components Visible.
    	 * If it is set to "false", the components in the container will not be visible.
    	 */
    	setVisible(true);
    	setResizable(false); //Set the window to a permanent size
    	setLocation(285,200); //Set the window to a specific location (X Axis, Y Axis)
    }
    
	//Main Method
    public static void main (String[] args) {
    	displayInterface di = new displayInterface();
	}
}

Required Image(s):




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