advance java technology java’s role::mvc architecture :: swing prof.chintan dave

32
Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Da

Upload: margaret-palin

Post on 02-Apr-2015

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Advance Java Technology

Java’s Role::MVC Architecture :: Swing

Prof.Chintan Dave

Page 2: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Who is the Founder of Java???????

Page 3: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Which is the Most Popular Machine Used in Day to Day Life Uses Java????????

Page 4: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Which is the Most Popular OS Uses Java as Prime Framework ???

Page 5: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Most Popular Companies uses Java as Prime Technology…

GoogleYahooMicrosoftLinkedIn……………..endless list

Page 6: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Usage wise Programming Languages/Technologies

Page 7: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Common Architecture for Any Programming Technology

Operating System

Run Time Environment

Class Libraries

Application

Page 8: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Java Architecture

Operating System- Windows,Mac,Solaris…

Run Time Environment-JVM

Class Libraries-Packages

Application-Windows ,Web , Mobile …….

Page 9: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

How Application Can be Developed

- Traditional Way of Developing an Application with Forms…Approach

-Developing an Application with MVC Architecture

None is Best None is Worst Too--- Its Just on Requirements… or Approach for an Application

Page 10: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

MVC Architecture

a software design pattern for developing web applications.

Model - The lowest level of the pattern which is responsible for maintaining data.

View - This is responsible for displaying all or a portion of the data to the user.Controller - Software Code that controls the interactions between the Model and View.

Page 11: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

What is Swing????

It is a SET of Programs which provides facility to Create GUI.

It is a Widget Toolkit It is Part of JFC(Java Foundation Classes It is Useful for Creating Rich Set of Desktop

Application

IT MEANS it’s a PURE JAVA

Page 12: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

What You Need to Run Java- SWING

• JAVA SE- which can be downloaded from java.sun.com/javase

To Make Java Code You’ll Need an Editor….Most Popular Once are…..

• Notepad• Eclipse• Net beans• JCreator

Page 13: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Pluggable Look n Fell

Pluggable L&F (PLAF) is basically a "skin" system for Swing components - you can change the style of your application by changing its LAF

Allows an Application Development to Highly Interactive GUI Component and Effects with ready made set of APIs

Page 14: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Applet Vs Swing

Swing Applet

Swing have its own Layout like most popular Box Layout.

Applet uses AWT Layouts like flowlayout.

Swing is light weight Component.

iTS Heavy Weight

Swing uses for stand lone Applications, Swing have main method to execute the program

Applet need HTML code for Run the Applet

Swing Uses MVC Applet Does not

Page 15: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Swing Features

• Lightweight - Swing component are independent of native Operating System's API as Swing API controls are rendered mostly using pure JAVA code instead of underlying operating system calls.

• Rich controls - Swing provides a rich set of advanced controls like Tree, Tabbed Pane, slider, color picker, table controls

• Highly Customizable - Swing controls can be customized in very easy way as visual appearance is independent of internal representation.

Page 16: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

To Use Swing Components..• Create an Object of Component For Example: JButton but = new JButton();

• Adding Component For Example:

Container cp = getContentPane();

cp.add(new JButton(“but”)); • Laying Out Component Not so difficult but takes a little practice JPanel p1 = new JPanel(); p1.setLayout(new FlowLayout(FlowLayout.LEFT));

Page 17: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Simple Swing Exampleimport javax.swing.*; public class Simple { JFrame f; Simple(){ f=new JFrame(); JButton b=new JButton("click"); b.setBounds(130,100,100, 40); f.add(b); f.setSize(400,500); f.setLayout(null); f.setVisible(true);

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

Page 18: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

JLabel

The class JLabel can display either text, an image, or bothExample code:

import javax.swing.*;

class hello {public static void main(String[] args){

JFrame f = new JFrame(“title”);JPanel p = new JPanel();JLabel b = new JLabel(“Demo Label”);p.add(b); // add labelto panelf.setContentPane(p); // add panel to framef.show();

}}

Page 19: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

JButtonThe class JButton is an implementation of a push button. It can have Image also

Page 20: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Code - JButtonimport javax.swing.*;public class SimpleButton {JFrame f; SimpleButton(){f=new JFrame("Simple Button

Example");

ImageIcon india = new ImageIcon("india.jpg");

JButton jb = new JButton("Our Flag",india);

JButton b=new JButton("click");f.add(b);f.add(jb);b.setBounds(130,100,100, 40);jb.setBounds(130,200,500,200);

f.setSize(400,500);f.setLayout(null);f.setVisible(true);f.setDefaultCloseOperation(JF

rame.EXIT_ON_CLOSE);}public static void

main(String[] args){new SimpleButton();}}

Page 21: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

JTextfieldThe class JTextField is a component which allows the editing of a

single line of text.Common Constructors:-• JTextField(String text):- Constructs a new TextField initialized with the specified text.

Page 22: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Code - JTextFieldimport java.awt.*;import java.awt.event.*;import javax.swing.*;//code that demonstrate use of

JLabel,JButton,JTextfield and Sample GUI Application

public class SimpleTextField {JFrame f; SimpleTextField(){ //Setting Layout f=new JFrame("Simple Button

Example"); JPanel controlPanel = new

JPanel(); controlPanel.setLayout(new

FlowLayout());

//Creating GUI Aspects for an Appllication JLabel lblUsername= new JLabel("Username:", JLabel.RIGHT); JLabel lblPassword = new JLabel("Password:", JLabel.CENTER); JTextField txtUsername = new JTextField(10); JTextField txtPassword = new JTextField(10); JButton btnLogin=new JButton("Login");

Page 23: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Code – JTextField-Continue//Adding Instance in Layout

controlPanel.add(lblUsername);

controlPanel.add(txtUsername);

controlPanel.add(lblPassword);

controlPanel.add(txtPassword);

controlPanel.add(btnLogin); f.add(controlPanel); f.setSize(400,500); f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String[] args){new SimpleTextField(); //calling GUI

} }

Page 24: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Event Handling

• What is an Event? Change in the state of an object is known as event.• Types of Event• The events can be broadly classified into two

categories:• Foreground Events - Those events which require

the direct interaction of user.• Background Events - Those events that require the

interaction of end user are known as background events. Operating system interrupts

Page 25: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Code for Simple Even Handling import java.awt.*;import java.awt.event.*;import javax.swing.*;//code that demonstrate use of

JLabel,JButton,JTextfield and Sample GUI Application

public class SimpleButtonEvent {JFrame f; JLabel statusLabel=new JLabel("Your Status");

SimpleButtonEvent(){ //Setting Layout f=new JFrame("Simple Button Example"); JPanel controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout());

//Creating GUI Aspects for an Appllication JButton btnOk=new JButton("Ok"); JButton btnCancle=new JButton("Cancle"); JButton btnRetry=new JButton("Retry");

//Adding Instance in Layout controlPanel.add(btnOk); controlPanel.add(btnCancle); controlPanel.add(btnRetry);btnOk.addActionListener(new

ButtonClickListener()); btnCancle.addActionListener(new

ButtonClickListener()); btnRetry.addActionListener(new

ButtonClickListener());

controlPanel.add(statusLabel); f.add(controlPanel); f.setSize(400,500); f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

Page 26: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Code for Simple Even Handling private class ButtonClickListener implements

ActionListener{ public void actionPerformed(ActionEvent e)

{

String command = e.getActionCommand(); if( command.equals("Ok")) { statusLabel.setText("Ok Button clicked."); } else if( command.equals("Cancle") ) { statusLabel.setText("Cancle Button

clicked."); } else { statusLabel.setText("Retry Button

clicked."); } }}

public static void main(String[] args){ new SimpleButtonEvent(); //calling GUI

} }

Page 27: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

JCheckBox• The class JCheckBox is an implementation of a check box - an item that can be selected or

deselected, and which displays its state to the user.

• Constructor- JCheckBox(String text): Creates an initially unselected check box with text.

• Method- getStateChange() – A Boolean Method to get the state of CheckBox

• Event- Listener Is – ItemListener Event is - ItemEvent

Page 28: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Code for Simple Checkboximport java.awt.*;import java.awt.event.*;import javax.swing.*;//code that demonstrate use of JLabel,JButton,JTextfield and

Sample GUI Application

public class SimpleCheckBox {JFrame f; SimpleCheckBox(){ //Setting Layout f=new JFrame("Simple CheckBox Example"); JPanel controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout());

//Creating GUI Aspects for an Appllication JCheckBox chkApple = new JCheckBox("Apple"); JCheckBox chkMango = new JCheckBox("Mango"); JCheckBox chkBanana = new JCheckBox("Banana");

//Adding Instance in Layout controlPanel.add(chkApple); controlPanel.add(chkMango); controlPanel.add(chkBanana);

chkApple.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { f.setTitle("Apple Checkbox: " +

(e.getStateChange()==1?"checked":"unchecked")); } }); f.add(controlPanel); f.setSize(400,500); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}

public static void main(String[] args){new SimpleCheckBox(); //calling GUI

} }

Page 29: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

JRadioButton

• Same as Checkbox But……Usage…is Different as per GUI’s Needs

Page 30: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

JList

• The class JList is a component which displays a list of objects and allows the user to select one or more item

• Constructor:- JList()- Constructs a JList with an empty, read-only, model.

• Method:- addElement(String) – To add an Element in a List setSelectionMode()- selection mode for the list

Page 31: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

Code for Simple Listimport java.awt.*;import java.awt.event.*;import javax.swing.*;//code that demonstrate use of JLabel,JButton,JTextfield and

Sample GUI Application

public class SimpleCheckBox {JFrame f; SimpleCheckBox(){ //Setting Layout f=new JFrame("Simple CheckBox Example"); JPanel controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout());

//Creating GUI Aspects for an Appllication JCheckBox chkApple = new JCheckBox("Apple"); JCheckBox chkMango = new JCheckBox("Mango"); JCheckBox chkBanana = new JCheckBox("Banana");

//Adding Instance in Layout controlPanel.add(chkApple); controlPanel.add(chkMango); controlPanel.add(chkBanana);

chkApple.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { f.setTitle("Apple Checkbox: " +

(e.getStateChange()==1?"checked":"unchecked")); } }); f.add(controlPanel); f.setSize(400,500); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}

public static void main(String[] args){new SimpleCheckBox(); //calling GUI

} }

Page 32: Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave

JComboBox

• Same as Jlist but Usage is Different- Usage is As per GUI NEED