0% found this document useful (0 votes)
80 views45 pages

Chapter Four Java Swing

Java Swing is a GUI widget toolkit for Java that improves upon AWT. It provides platform-independent and lightweight components. Some key Swing components include JButton, JTextField, JTextArea. Swing supports pluggable look and feel, provides more powerful components than AWT, and has a hierarchy of container classes like Window, Panel, and Frame.

Uploaded by

lutfullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views45 pages

Chapter Four Java Swing

Java Swing is a GUI widget toolkit for Java that improves upon AWT. It provides platform-independent and lightweight components. Some key Swing components include JButton, JTextField, JTextArea. Swing supports pluggable look and feel, provides more powerful components than AWT, and has a hierarchy of container classes like Window, Panel, and Frame.

Uploaded by

lutfullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Chapter Four

Java Swing
Java Swing
Java Swing is a package in java which is used to create window-based
applications. It is built on the top of AWT (Abstract Windowing Toolkit)
API and entirely written in java.
Unlike AWT, Java Swing provides platform-independent and lightweight
components.
The javax.swing package provides classes for java swing API such as
JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu,
JColorChooser etc.
Difference between AWT and Swing
No. Java AWT Java Swing

1) AWT components are platform-dependent. Java swing components are platform-independent.

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

3) AWT doesn't support pluggable look and feel. Swing supports pluggable look and feel.

4) AWT provides less components than Swing. Swing provides more powerful components such as


tables, lists, colorchooser, etc.
Hierarchy of Java Swing classes
Types of containers
There are four types of containers in Java AWT:
1. Window
2. Panel
3. Frame
4. Dialog
Types of containers
Window
The window is the container that have no borders and menu bars. You must
use frame or dialog for creating a window.
We need to create an instance of Window class to create this container.
Panel
The Panel is the container that doesn't contain title bar, border or menu bar.
It can have other components like button, text field etc. An instance of Panel
class creates a container, in which we can add components.
Types of containers
Frame
The Frame is the container that contain title bar and border and can
have menu bars.
It can have other components like button, text field, scrollbar etc.
Frame is most widely used container while developing an AWT
application.
Useful Methods of Component Class

Method Description

public void add(Component c) Inserts a component on this component.

public void setSize(int width,int height) Sets the size (width and height) of the
component.

public void setLayout(LayoutManager m) Defines the layout manager for the component.

public void setVisible(boolean status) Changes the visibility of the component, by


default false.
Java AWT Example
To create simple AWT example, you need a frame. There are two
ways to create a GUI using Frame in AWT.

1. By extending Frame class (inheritance)


2. By creating the object of Frame class (association)
import java.awt.*;    
1. AWT Example by Inheritance
public class AWTExample1 extends Frame {    
AWTExample1() {  
Button b = new Button("Click Me!!");  
b.setBounds(30,100,80,30);  
add(b);  
setSize(300,300);  
setTitle("This is our basic AWT example");   
setLayout(null);   
setVisible(true);  }    
public static void main(String args[]) {   
AWTExample1 f = new AWTExample1();    }  }    
Output
2. AWT Example by Association
import java.awt.*;    
class AWTExample2 {    
AWTExample2() {  
Frame f = new Frame();  
      Label l = new Label("Employee id:");   
      Button b = new Button("Submit");  
      TextField t = new TextField();  
      l.setBounds(20, 80, 80, 30);  
      t.setBounds(20, 100, 80, 30);  
      b.setBounds(100, 100, 80, 30);  
  
2. AWT Example by Association (Con..)
      f.add(b);  
      f.add(l);  
      f.add(t);  
      f.setSize(400,300);  
      f.setTitle("Employee info");   
      f.setLayout(null);   
      f.setVisible(true);  }    
  public static void main(String args[]) {   
  AWTExample2 awt_obj = new AWTExample2();    
  }  }
Output
Java AWT Button
•A button is basically a control component with a label that generates
an event when pushed.

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


independent implementation.

•The application result in some action when the button is pushed.

•To create a Button we need to create the object of Button Class.


Java AWT Button Example
import java.awt.*;    
public class ButtonExample {    
public static void main (String[] args) {   
    Frame f = new Frame("Button Example");      
    Button b = new Button("Click Here");   
    b.setBounds(50,100,80,30);    
    f.add(b);      
    f.setSize(400,400);    
    f.setLayout(null);    
    f.setVisible(true);     }    }   
Output
import java.awt.*;    
import java.awt.event.*;    
public class ButtonExample3 {     Java AWT Button Example with ActionListener
public static void main(String[] args) {    
    Frame f = new Frame("Button Example");    
    final TextField tf=new TextField();    
    tf.setBounds(50,50, 150,20);    
    Button b=new Button("Click Here");    
    b.setBounds(50,100,60,30);   
    b.addActionListener(new ActionListener() {    
    public void actionPerformed (ActionEvent e) {    
            tf.setText("Welcome to Javatpoint.");         }    });  
    f.add(b);   f.add(tf);       
    f.setSize(400,400);     f.setLayout(null);    
    f.setVisible(true);     }    }   
Output
Java AWT Label
•The object of the Label 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 a programmer but a user cannot edit it directly.
•To create a label, we need to create the object of Label class.
 
import java.awt.*;    
public class LabelExample {    
public static void main(String args[]){      Java AWT Label Example
    Frame f = new Frame ("Label example");  
    Label l1, l2;    
    l1 = new Label ("First Label.");   
    l2 = new Label ("Second Label.");     
    l1.setBounds(50, 100, 100, 30);    
    l2.setBounds(50, 150, 100, 30);  
    f.add(l1);   f.add(l2);     
    f.setSize(400,400);    
    f.setLayout(null);    
    f.setVisible(true);    }    }  
Output
Java AWT TextField
The object of a TextField class is a text component that allows a user
to enter a single line text and edit it.

When we enter a key in the text field (like key pressed, key released
or key typed), the event is sent to TextField.

To create a text field we have to create the object of TextField Class.
Java AWT TextField Example
import java.awt.*;   
public class TextFieldExample {  
    public static void main(String args[]) {     
    Frame f = new Frame("TextField Example");    
    TextField t1;      
    t1 = new TextField();    
    t1.setBounds(50, 100, 200, 30);     
    f.add(t1);  
    f.setSize(400,400);    
    f.setLayout(null);    
    f.setVisible(true);    
}    }  
Assignment
Write a java program using java AWT to create a login page?
Java AWT TextArea
The object of a TextArea class is a multiline region that displays text.
It allows the editing of multiple line text.
The text area allows us to type as much text as we want. When the
text in the text area becomes larger than the viewable area, the
scroll bar appears automatically which helps us to scroll the text up
and down, or right and left.
To create a text area create an object of TextArea Class.
Java AWT TextArea Example
import java.awt.*;    
public class TextAreaExample    {    
     TextAreaExample() {    
        Frame f = new Frame();    
            TextArea area = new TextArea("Welcome to javapoint");    
        area.setBounds(10, 30, 300, 300);     
        f.add(area);  
        f.setSize(400, 400);    
        f.setLayout(null);    
        f.setVisible(true);         }    
public static void main(String args[])    {    
   new TextAreaExample();    
}    } 
Java AWT 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".
Java AWT Checkbox Example
import java.awt.*;    
public class CheckboxExample1  {    
     CheckboxExample1() {    
       Frame f = new Frame("Checkbox Example");    
        Checkbox checkbox1 = new Checkbox("C++");    checkbox1.setBounds(100, 100,  50, 50);    
        Checkbox checkbox2 = new Checkbox("Java", true);           checkbox2.setBounds(100, 150,  50, 50);    
        f.add(checkbox1);    f.add(checkbox2);    
        f.setSize(400,400);    
        f.setLayout(null);    
        f.setVisible(true);    }    
public static void main (String args[])    {    
    new CheckboxExample1();    }    }   
Java AWT CheckboxGroup
The object of CheckboxGroup class is used to group together a set
of Checkbox.
At a time only one check box button is allowed to be in "on" state
and remaining check box button in "off" state.
Java AWT CheckboxGroup Example
import java.awt.*;    
public class CheckboxGroupExample    {    
       CheckboxGroupExample(){    
       Frame f= new Frame("CheckboxGroup Example");    
        CheckboxGroup cbg = new CheckboxGroup();  
        Checkbox checkBox1 = new Checkbox("C++", cbg, false);    
        checkBox1.setBounds(100,100, 50,50);    
        Checkbox checkBox2 = new Checkbox("Java", cbg, true);    
        checkBox2.setBounds(100,150, 50,50);    
        f.add(checkBox1);    
        f.add(checkBox2);    
        f.setSize(400,400);    
        f.setLayout(null);    
        f.setVisible(true);    
     }    
public static void main(String args[])    
{    
    new CheckboxGroupExample();    
}    
}  
Java AWT Choice
The object of Choice class is used to show popup menu of
choices. Choice selected by user is shown on the top of a
menu.

To create choices create an object of Choice Class.


Java AWT Choice Example
In the following example, we are creating a choice menu
using Choice() constructor.
Then we add 5 items to the menu using add() method and
Then add the choice menu into the Frame.
Java AWT Choice Example
import java.awt.*;   
public class ChoiceExample1 {    
        ChoiceExample1() {    
        Frame f = new Frame();    
        Choice c = new Choice();   
          c.setBounds(100, 100, 75, 75);    
        c.add("Item 1");    
        c.add("Item 2");    
        c.add("Item 3");    
        c.add("Item 4");    
        c.add("Item 5");     
Java AWT Choice Example
        f.add(c);    
        f.setSize(400, 400);    
        f.setLayout(null);    
        f.setVisible(true);    
     }    
public static void main(String args[])    
{    
   new ChoiceExample1();    
}    
}    
Output
Java AWT Scrollbar
The object of Scrollbar class is used to add horizontal and
vertical scrollbar.
Scrollbar is a GUI component allows us to see invisible
number of rows and columns.
Example
import java.awt.*;   
public class ScrollbarExample1 {    
ScrollbarExample1() {    
            Frame f = new Frame("Scrollbar Example");    
            Scrollbar s = new Scrollbar();    
            s.setBounds (100, 100, 50, 100);  
            f.add(s);     
            f.setSize(400, 400);  
            f.setLayout(null);    
            f.setVisible(true);    }    
public static void main(String args[]) {    
       new ScrollbarExample1();    }    }  
Output
Java AWT MenuItem and Menu
The object of MenuItem class adds a simple labeled menu
item on menu. The items used in a menu must belong to the
MenuItem or any of its subclass.
The object of Menu class is a pull down menu component
which is displayed on the menu bar.
Java AWT MenuItem and Menu Example
import java.awt.*;  
class MenuExample  {  
     MenuExample(){  
         Frame f= new Frame("Menu and MenuItem Example");  
         MenuBar mb=new MenuBar();  
         Menu menu=new Menu("Menu");  
         Menu submenu=new Menu("Sub Menu");  
         MenuItem i1=new MenuItem("Item 1");  
         MenuItem i2=new MenuItem("Item 2");  
         MenuItem i3=new MenuItem("Item 3");  
         MenuItem i4=new MenuItem("Item 4");  
         MenuItem i5=new MenuItem("Item 5");  
 
Java AWT MenuItem and Menu Example
         menu.add(i1);  
         menu.add(i2);  
         menu.add(i3);  
         submenu.add(i4);  
         submenu.add(i5);  
         menu.add(submenu);  
         mb.add(menu);  
         f.setMenuBar(mb);  
         f.setSize(400,400);  
         f.setLayout(null);  
         f.setVisible(true);  }  
public static void main(String args[])  
{  new MenuExample();  }  }
Output
End of Chapter

You might also like