31
GUI Programing with Java Java Provides 2 Frameworks for building GUI-based applications. Those are AWT-(Abstract Window Toolkit) Swing

GUI Programing with Java

  • Upload
    others

  • View
    27

  • Download
    0

Embed Size (px)

Citation preview

Page 1: GUI Programing with Java

GUI Programing with Java

Java Provides 2 Frameworks for building GUI-basedapplications. Those are

• AWT-(Abstract Window Toolkit)

• Swing

Page 2: GUI Programing with Java

AWT-(Abstract Window Toolkit)

• AWT (Abstract Window Toolkit) is an API to develop GUI orwindow-based applications in java.

• The java.awt package provides classes for AWT api such asTextField, Label, TextArea, CheckBox, Choice, List etc.

• AWT components are platform-dependent i.e. componentsare displayed according to the view of operating system.

Page 3: GUI Programing with Java

AWT Hierarchy

Page 4: GUI Programing with Java

• Component:

Component is an abstract class that contains various classes such as

Button, Label,Checkbox,TextField,Menu and etc.

• Container:

The Container is a component in AWT that can contain another

components like buttons, textfields, labels etc. The Container class extendsFrame and Panel.

• Window:The window is the container that have no borders and menu bars.

You must use frame for creating a window.

• Frame:The Frame is the container that contain title bar and can have menu

bars. It can have other components like button, textfield etc.

• Panel:The Panel is the container that doesn't contain title bar and menu

bars. It can have other components like button, textfield etc.

Page 5: GUI Programing with Java

Useful Methods of Component class

Method Description

add(Component c) inserts a component on this component.

setSize(int width,int height) sets the size (width and height) of thecomponent.

setLayout(LayoutManager m) defines the layout manager for thecomponent.

setVisible(boolean status) changes the visibility of the component, bydefault false.

setTitle(String text) Sets the title for component

Page 6: GUI Programing with Java

To create simple awt example, you need a frame. There are two ways to create a frame in AWT.

• By extending Frame class (inheritance)Ex: class Example extends Frame{

……..}

• By creating the object of Frame class (association)Ex:class Example {Frame obj=new Frame();

……..}

Page 7: GUI Programing with Java

• A Simple AWT Example:

SimpleExample.java

import java.awt.*;

class SimpleExample extends Frame

{

SimpleExample()

{

setSize(300,300);//frame size 300 width and 300 height

setLayout(null); //no layout manager

setVisible(true);//now frame will be visible, by default not visible

setTitle("SimpleExample");//Set Title

}

public static void main(String args[]){

SimpleExample f=new SimpleExample();

}}

Page 8: GUI Programing with Java

AWT Components or Elements

• Button:The button class is used to create a labeled button that has platform

independent implementation. The application result in some action when thebutton is pushed.

Syntax:

Button b=new Button(“Text");

(Or)

Button b1,b2;

b1=new Button(“Text”);

b.setBounds(50,100,80,30);

setBounds(int x,int y,int width,int height)This method is used to declare location ,width & height of all

components of AWT. X Y

Example: setBounds(50,100,80,30);

width Height

Page 9: GUI Programing with Java

• Label:

The Label class is a component for placing text in a container. It is usedto display a single line of read only text. The text can be changed by an applicationbut a user cannot edit it directly.

Syntax:

Label l1=new Label(“Text”);

(or)

Label l1,l2;

l1=new Label(“Text”);

• TextField:The TextField class is a text component that allows the editing of a single

line text.

Syntax:

TextField t1=new TextField(“Text”);

(or)

TextField t1,t2;

t1=new TextField(“Text”);

Page 10: GUI Programing with Java

• TextArea :

The TextArea class is a multi line region that displays text. It allows theediting of multiple line text.

Syntax:

TextArea t1=new TextArea(“Text”);

(or)

TextArea t1,t2;

t1=new TextArea(“Text”);

• Checkbox :The Checkbox class is used to create a checkbox. It is used to turn an

option on (true) or off (false). Clicking on a Checkbox changes its state from "on"to "off" or from "off" to "on".

Syntax:

Checkbox c1=new Checkbox(“Text”);

(or)

Checkbox c1,c2;

c1=new Checkbox(“Text”);

Page 11: GUI Programing with Java

• Choice :

The Choice class is used to show popup menu of choices. Choiceselected by user is shown on the top of a menu.

Syntax:

Choice c=new Choice();

c.add("Item 1");

c.add("Item 2");

c.add("Item 3");

• List :The List class represents a list of text items. By the help of list, user

can choose either one item or multiple items.

Syntax:

List ls=new List(Size);

ls.add("Item 1");

ls.add("Item 2");

ls.add("Item 3");

Page 12: GUI Programing with Java

• AWT does allow the user to close window directly…

• We need code to close window

//Code for Close window

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent we)

{

System.exit(0);

}

});

Note: We need to import one package “java.awt.event.*”.

Page 13: GUI Programing with Java

Event Handling• Event: Changing the state of an object(component)is known as an

event. For example, click on button, dragging mouse etc.

• Event describes the change in state of component. Events aregenerated as result of user interaction with the graphical userinterface components. For example, clicking on a button, movingthe mouse, entering a character through keyboard and selecting anitem from list.

Def: Event Handling is the mechanism that controls the eventand decides what should happen if an event occurs.

• This mechanism have the code which is known as event handlerthat is executed when an event occurs.

• The java.awt.event package provides many event classes andListener interfaces for event handling.

Page 14: GUI Programing with Java

Steps to perform Event Handling

Following steps are required to perform event handling:

– Register the component with the Listener.

By using addActionListener(ActionListener a)

Example:

Button b=new Button(“Submit”);

b.setBounds(100,50,80,30);

b.addActionListener(this);

– Provide or put event handling code.

By using actionPerformed(ActionEvent e) method of ActionListenerInterface.

Example:

Public void actionPerformed(ActionEvent e)

{

tf.setText(“welcome”);

}

Page 15: GUI Programing with Java

Simple Example:

import java.awt.*; import java.awt.event.*; class EventExample extends Frame implements ActionListener{ TextField tf;EventExample(){

tf=new TextField(); //create components tf.setBounds(60,50,170,20); Button b=new Button("click me"); b.setBounds(100,120,80,30); b.addActionListener(this);//register listener &passing current instance add(b);add(tf); //add components and set size, layout and visibility setSize(300,300); setLayout(null); setVisible(true); } public void actionPerformed(ActionEvent e){ tf.setText("Welcome"); } public static void main(String args[]){ new EventExample(); } }

Page 16: GUI Programing with Java

Swing

• Swing is a framework or API that is used to create GUI (or)window-based applications.

• It is an advanced version of AWT (Abstract Window Toolkit)API and entirely written in java.

• Unlike AWT, Java Swing provides platform-independent andlightweight components.

• The javax.swing package provides classes for java swing APIsuch as JButton, JTextField, JTextArea, JRadioButton,JCheckbox, JMenu, JColorChooser etc.

Page 17: GUI Programing with Java

Difference between AWT and Swing

There are many differences between java awt and swing that are given below.

No. AWT Swing

1) AWT components are platform-dependent.

Swing components are platform-independent.

2) AWT components are heavyweight. Swing components are lightweight.

3) AWT provides less components than Swing.

Swing provides more powerful components such as tables, lists, scrollpanes, colorchooser and` etc.

4) AWT doesn't follows MVC(Model ViewController) where model representsdata, view represents presentation andcontroller acts as an interface betweenmodel and view.

Swing follows MVC.

Page 18: GUI Programing with Java

Swing Hierarchy

Page 19: GUI Programing with Java

Commonly used Methods of Component class

Method Description

add(Component c) inserts a component on this component.

setSize(int width,int height) sets the size (width and height) of thecomponent.

setLayout(LayoutManager m) defines the layout manager for thecomponent.

setVisible(boolean status) changes the visibility of the component, bydefault false.

setTitle(String text) Sets the title for component

Page 20: GUI Programing with Java

To create simple swing example, you need a frame.

In swing, we use JFrame class to create a frame.

There are two ways to create a frame in swing.

• By extending JFrame class (inheritance)Ex: class Example extends JFrame{

……..}

• By creating the object of JFrame class (association)Ex:class Example {JFrame obj=new JFrame();

……..}

Page 21: GUI Programing with Java

A Simple Swing Example

We can write the code of swing inside the main() or constructor.

In Main() Method:

SwingExample.java

import javax.swing.*;

public class SwingExample

{

public static void main(String[] args)

{

JFrame f=new JFrame("Simple Swing Example");

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

}

}

Page 22: GUI Programing with Java

In Constructor()

import javax.swing.*;

class SimpleExample extends JFrame

{

SimpleExample()

{

setSize(300,300);//frame size 300 width and 300 height

setLayout(null);//no layout manager

setVisible(true);//now frame will be visible, by default not visible

setTitle("SimpleExample");//Set Title

}

public static void main(String args[]){

SimpleExample f=new SimpleExample();

}

}

Page 23: GUI Programing with Java

AWT vs Swing

Page 24: GUI Programing with Java

Components of SwingJButton:The JButton class is used to create a labeled button that has platformindependent implementation. The application result in some action when thebutton is pushed.

Syntax:JButton b=new JButton(“Text");(Or)JButton b1,b2;b1=new JButton(“Text”);b.setBounds(50,100,80,30);

JLabel:The JLabel class is a component for placing text in a container. It is used

to display a single line of read only text. The text can be changed by an applicationbut a user cannot edit it directly.

Syntax:JLabel l1=new JLabel(“Text”);(or)JLabel l1,l2;l1=new JLabel(“Text”);

Page 25: GUI Programing with Java

JTextField:The JTextField class is a text component that allows the editing of a

single line text.

Syntax:

JTextField t1=new JTextField(“Text”);

(or)

JTextField t1,t2;

t1=new JTextField(“Text”);

JTextArea :The JTextArea class is a multi line region that displays text. It allows

the editing of multiple line text.

Syntax:

JTextArea t1=new JTextArea(“Text”);

(or)

JTextArea t1,t2;

t1=new JTextArea(“Text”);

Page 26: GUI Programing with Java

JCheckBox :The JCheckBox class is used to create a checkbox. It is used to turn an

option on (true) or off (false). Clicking on a Checkbox changes its state from "on"to "off" or from "off" to "on".

Syntax:JCheckBox c1=new JCheckBox(“Text”);(or)JCheckBox c1,c2;c1=new JCheckBox(“Text”);

JListThe object of JList class represents a list of text items. The list of text

items can be set up so that the user can choose one or more items from list of items.

Syntax:DefaultListModel<String> l1 = new DefaultListModel<>(); l1.addElement("Item1"); l1.addElement("Item2"); l1.addElement("Item3"); l1.addElement("Item4"); JList list = new JList<>(l1);

Page 27: GUI Programing with Java

JPasswordField:

The JPasswordField class is a text component specialized for

password entry. It allows the editing of a single line of text.

Syntax:

JPasswordField pwd = new JPasswordField();

pwd.setBounds(100,50,80,30);

JRadioButtonThe JRadioButton class is used to create a radio button. It is used to

choose one option from multiple options. It is widely used in exam systems orquiz.

• It should be added in ButtonGroup to select one radio button only.

Syntax:

ButtonGroup bg=new ButtonGroup();

JRadioButton r1=new JRadioButton("Male");

JRadioButton r2=new JRadioButton("Female");

bg.add(r1);bg.add(r2);

Page 28: GUI Programing with Java

JComboBox:

The JComboBox class is used to show popup menu of items. Itemselected by user is shown on the top of a menu.(like Choice class in AWT)

Syntax:

String country[]={"India","Aus","U.S.A","England","Newzealand"};JComboBox cb=new JComboBox(country);

cb.setBounds(50, 50,90,20);

JTable:The JTable class is used to display data in tabular form. It is composed of

rows and columns.

Syntax:

String data[][]= { {“521",“Madhu",“43400"},

{“512",“Hari",“54500"},

{“509",“Ganesh","70000"}};

String column[]={"ID","NAME","SALARY"};

JTable jt=new JTable(data,column);

jt.setBounds(30,40,200,300);

Page 29: GUI Programing with Java

JOptionPane:

The JOptionPane class is used to provide standard dialog boxes suchas message dialog box, confirm dialog box and input dialog box. These dialogboxes are used to display information or get input from the user.

Syntax:

JFrame f=new JFrame();

JOptionPane.showMessageDialog(f,"Hello, Welcome to Swings.");

String name=JOptionPane.showInputDialog(f,"Enter Name");

int a=JOptionPane.showConfirmDialog(f,"Are you sure?");

JProgressBar:The JProgressBar class is used to display the progress of the task.

Syntax:

JProgressBar jb=new JProgressBar(0,200);

jb.setBounds(40,40,160,30);

jb.setValue(0);

jb.setStringPainted(true);

Page 30: GUI Programing with Java

JPanel:

The JPanel is a simplest container class. It provides space in which anapplication can attach any other component.

Syntax:

JPanel panel=new JPanel();

panel.setBounds(40,80,200,200);

panel.setBackground(Color.gray);

JButton b1=new JButton("Button 1");

b1.setBounds(50,100,80,30);

panel.add(b1);

JDialog:The JDialog control represents a top level window with a border and a

title used to take some form of input from the user.

• Unlike JFrame, it doesn't have maximize and minimize buttons.

Syntax:

JFrame f= new JFrame();

JDialog d=new JDialog(f , "Dialog", true);

JButton b = new JButton ("OK");

d.add(b);

Page 31: GUI Programing with Java