Advertisement :
   Log In    OR    Register  
  Topics :  
RMI Example

Home >>> Coding Tips on Java >>> Java Framework >>> Using Swing API
Struts Tutorials:
Struts2 Tag CheckBoxList
, Checkbox, Iterator, IF
Struts2 Tag Library Example Struts2 Tiles Example Struts2 Tiles I18N Example Struts2 Questions Struts Tiles I18N Example Struts Eclipse MVC Struts2 Tags Struts2 Example and Tutorial Struts MVC Struts2 Validation
Hibernate Tutorials: Hibernate Case Study Class Hierarchy Persist Example Using Hibernate Interceptor Hibernate Questions with Answer Hibernate Many-to-Many Mapping Example Hibernate one-to-many Mapping Example Hibernate and ORM tools Spring Hibernate Example Hibernate SessionFactory Example Hibernate Mapping Class Hierarchy Hibernate Questions Hibernate SessionFactory Questions Spring Hibernate Example: Spring Hibernate Case Study

Written By : Amit
Title :
Event handling and Java Technology
Description : Event handling and Java Technology
More...


Written By : admin
Title :
Using Final Keyword
Description : Example on using Final Keyword in Java code
More...


Written By : Amit
Title :
In memory AWT Image Creation
Description : Java Code
More...


Written By : Amit
Title :
Java Integral Types
Description : Java Types with Bit size
More...


Written By : Amit
Title :
Search Class in JAR
Description : Using Java Technology
More...


Written By : Amit
Title :
Move component on Java Applet
Description : Drag and Drop
More...


Written By : Amit
Title :
ArrayBlockingQueue and Queue
Description : Using Java Technology
More...


Written By : Amit
Title :
Auto generation of code using Java
Description : Castor XSD
More...

Tags/Keywords : Java Framework, Example, Swing, Wizard, Framework
Author : Amit
Date (Year/Month/Date): 2010-05-11 Wizard Screen example code using Swing : trying to create a Framework using Java

Please be informed that NONE of the design/code/matter from this page is claiming to be some
sort of best practice and we DO NOT expect any of our visitor/reader of this page to assume
this as some sort of best practice for any context and should not be using this as it is
without appropriate evaluation.
This page intends only to provide bits and pieces of known ways  for doing some sort of example
and may not be fit for any other purpose.

In spite of all precautions taken to provide accurate and avoid any typo in these pages,
there might be some issues like grammatical mistakes and typo being observed in these pages,
We extend our sincerest apologies for the same.


A very simple demonstration of a forming a wizard of screens using Java's Swing API. This example will walk you through this example code, whereby there will be set of three buttons shown in the bottom section, and a dialog screen will be populated based on the user's selection of Next/ Previous Buttons. Finish button will only be enabled when there is the last screen being shown on screen. Source code is as follows: WizardFrame.java
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


/**
* Sample test program to show 13 frames using this Wizard
* component.
*
*/

public class WizardFrame extends JFrame
{
        public WizardFrame()
        {
	try{
		setSize(800,600);
		show();
		WizardModal wm = new WizardModal();
		wm.setSize(400,300);
		wm.setLocation(200,100);
		TestFrame tf1 = new TestFrame("One");
		tf1.setSize(100,100);
		TestFrame tf2 = new TestFrame("Two");
		tf2.setSize(100,100);
		TestFrame1 tf3 = new TestFrame1();
		tf3.setSize(100,100);
		TestFrame tf4 = new TestFrame("Four");
		tf4.setSize(300,400);
		TestFrame tf5 = new TestFrame("Five");
		tf5.setSize(300,400);
		TestFrame tf6 = new TestFrame("Six");
		tf6.setSize(300,400);
		TestFrame tf7 = new TestFrame("Seven");
		tf7.setSize(300,400);
		TestFrame tf8 = new TestFrame("Eight");
		tf8.setSize(300,400);
		TestFrame tf9 = new TestFrame("Nine");
		tf9.setSize(300,400);
		TestFrame tf10 = new TestFrame("Ten");
		tf10.setSize(300,400);
		TestFrame tf11 = new TestFrame("Eleven");
		tf11.setSize(300,400);
		TestFrame tf12 = new TestFrame("Twelve");
		tf12.setSize(300,400);
		TestFrame tf13 = new TestFrame("Thirteen");
		tf13.setSize(300,400);

		Object[] obj = new Object[13];
		obj[0] = tf1;
		obj[1] = tf2;
		obj[2] = tf3;
		obj[3] = tf4;
		obj[4] = tf5;
		obj[5] = tf6;
		obj[6] = tf7;
		obj[7] = tf8;
		obj[8] = tf9;
		obj[9] = tf10;
		obj[10] = tf11;
		obj[11] = tf12;
		obj[12] = tf13;

		wm.addPages(obj);
		wm.show();
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we)
			{
				System.exit(0);
			}
		});
	}catch(Exception ex){
		ex.printStackTrace();
	}
        }

        public static void main(String args[])
        {
                new WizardFrame();
        }
}


/**
* This is the modal dialog box that contains the Wizard screen.
*
*/

class WizardModal extends JDialog
{
        java.util.Vector pages = new java.util.Vector();
        static JPanel jp = new JPanel();
        static JPanel jp1 = new JPanel();
        JButton jbNext;
        JButton jbPrevious;
        JButton jbFinish;

        int pageCounter = 0;
        static WizardModal wm;
        boolean nextStatus = true;
        boolean previousStatus = true;
        boolean finishStatus = false;

        JPanel jp2;

        public WizardModal()
        {
	setModal(true);
	jbNext = new JButton("Next");
	jbPrevious = new JButton("Previous");
	jbFinish = new JButton("Finish");
	jp.add(jbNext);
	jp.add(jbPrevious);
	jp.add(jbFinish);
	this.getContentPane().add(jp,BorderLayout.SOUTH);
	this.getContentPane().add(jp1,BorderLayout.CENTER);
	validateButtons(pageCounter);
	wm = this;
	jbNext.addMouseListener(new MouseAdapter(){
	public void mouseClicked(MouseEvent me)
	{
		if(me.getSource() == jbNext && nextStatus)
		{
		if(jp2 instanceof TestFrame1)
		{
		  System.out.println(((TestFrame1)jp2).jtxt1.getText());
		}

		pageCounter++;
		decidePage(pageCounter);
		validateButtons(pageCounter);
		wm.show();
		}
	}
	});
	jbPrevious.addMouseListener(new MouseAdapter(){
	public void mouseClicked(MouseEvent me)
	{
		if(me.getSource() == jbPrevious && previousStatus)
		{
			pageCounter--;
			decidePage(pageCounter);
			validateButtons(pageCounter);
			wm.show();
		}
	}
	});

        }
        public void show()
        {
                super.show();
        }
        public void setLocation(int x, int y)
        {
                super.setLocation(x,y);
        }
        public void setSize(int w, int h)
        {
                super.setSize(w,h);
        }
        public void setPage(int index,Object page)
        {
                pages.set(index,page);
        }
        public Object getPage(int index)
        {
                return pages.get(index);
        }
        public Object firstPage()
        {
                return pages.firstElement();
        }
        public Object lastPage()
        {
                return pages.lastElement();
        }
        public Object pageAt(int index)
        {
                return pages.elementAt(index);
        }
        public void addPages(Object[] pages1)
        {
                for(int i=0;i<pages1.length;i++)
                {
                        pages.addElement(pages1[i]);
                }
        }

/**
*  Validating which button to show and which one to disable.
*
*/

        public void validateButtons(int pageCounter)
        {
                if(pageCounter == 0)
                {
                        jbPrevious.setEnabled(false);
                        previousStatus = false;
                        jbFinish.setEnabled(false);
                        finishStatus = false;

                } else{
                        jbPrevious.setEnabled(true);
                        previousStatus = true;
                }

                if(pageCounter == pages.size()-1)
                {
                        jbNext.setEnabled(false);
                        nextStatus = false;
                        jbFinish.setEnabled(true);
                        finishStatus = true;

                } else{

                        jbNext.setEnabled(true);
                        nextStatus = true;
                        jbFinish.setEnabled(false);
                        finishStatus = false;

                }

        }

/**
*  This is the method that decides which page to show.
*
*/

        public void decidePage(int pageCounter)
        {
              this.getContentPane().remove(1);
              jp2 = (JPanel)pageAt(pageCounter);
              this.getContentPane().add(jp2,BorderLayout.CENTER);
              this.setTitle("Page no:"+(pageCounter+1));
              this.getContentPane().update(jp2.getGraphics());
        }
}


/**
* This is a test frame to be enbedded onto the wizard screen.
*
*/


class TestFrame extends JPanel
{
        JLabel jlbl;
        String lbl;
        public TestFrame(String title)
        {
                lbl = title;
                jlbl = new JLabel(title);
                setLayout(new BorderLayout());
                add(jlbl,BorderLayout.SOUTH);
        }
}


/**
* This is a test frame to be enbedded onto the wizard screen.
*
*/

class TestFrame1 extends JPanel
{
        JLabel jlbl;
        public JTextField jtxt;
        public JLabel jlbl1;
        public JTextField jtxt1;
        JLabel jlbl2;
        JTextField jtxt2;

  public TestFrame1()
  {
	jlbl = new JLabel("Name : ");
	jtxt = new JTextField(10);
	jlbl1 = new JLabel("Address : ");
	jtxt1 = new JTextField(10);
	jlbl2 = new JLabel("Phone : ");
	jtxt2 = new JTextField(10);
	setLayout(new FlowLayout());
	add(jlbl);
	add(jtxt);
	add(jlbl1);
	add(jtxt1);
	add(jlbl2);
	add(jtxt2);
  }
}
In this source code I am using some of the deprecated method as well, as this example is not something usable in real project as it is, instead one can take some of the good thing from this code to start with, in order to comeup with some sort of better and advanced usable code/Framework. Initially I had thought of creating a Framework which can be used across multiple projects, may be released as open source, and usable enough to be plug and use kind of wizard Framework. So this example could be the starting point or a POC of this roadmap. In my free time I shall think of creating a robost design and a very loosely coupled framework having these screens, buttons, dialog boxes being separately customizable and easily integrated in this code wizard framework using Java Swing API. Any feedback/suggestions/discussion on this very topic will be highly appreciated.


	
Please write your Comment on this Matter
(This will be visible if found suitable):
Name: *
Email (will not be displayed): *
Matter: *
32,34
Enter bigger number from above :*
Home >>> Coding Tips on Java >>> Java Framework >>> Using Swing API
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
Referred By Name *:
Resource URL *: (e.g, URL should be starting with http://www.-----.---)
 
Resource Short Description *:
3,27
Enter bigger number from above : *

Please log in to add or reply to any matter<- requires login
Log in or Register
Copyright © 2008-2010, Interview-Questions-Tips-Forum, All Rights Reserved.
CONTACT    PRIVACY POLICY    DISCLAIMER
 

Oracle and Java are registered trademarks of Oracle and/or its affiliates.
Other names may be trademarks of their respective owners

This web site's Terms of Use and Disclaimer :

This web site provides some of the information about various technologies, example code, tips, tutorials etc. Like any printed materials,
content of these pages may become out of date over a period of time. Therefore all visitor/users of this web site are requested/advised
to refer to the originating parties/sources for the latest changes and happenings for detailed information. This information is not
intended to be a substitute for the original reference provided by the originating parties/sources. These examples with source code or
without source code, have not been thoroughly tested under all conditions. Interview-questions-tips-forum.net therefore, cannot guarantee
or imply reliability of these example source code or programs.

By accessing and using this website in any ways, including, without limitation, browsing the website pages, using any information, using
any content and/or downloading any materials, you agree to and are bound by the terms of use described in this page and Usage Terms and Conditions.
If you do not agree to all of the terms and conditions contained in the terms of use described in this page and
Usage Terms and Conditions, do not use this website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use on your employer's behalf.

All Trademarks are property of their respective owner. Appropriate measure is being taken for providing accurate and up-to-date
information but like any printed materials, these blog(s)/contents may eventually be outdated one day, so if you are using any of these
information, please refer original content/documentation from respective sources. And under no circumstances shall the Author of these
contents and/or this web site be liable for any loss, damage, expense incurred or suffered which is claimed to have occurred because of
usage of the contents of this web site. If you have any questions/queries/feedback/suggestions then please write to this web site owner
at contact.