Thursday, August 18, 2011

Display Image to another window using JRadioButton

Java E-Book How to Program Java

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):




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