Chapter Four Java Swing
Chapter Four Java Swing
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
3) AWT doesn't support pluggable look and feel. Swing supports pluggable look and feel.
Method Description
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.
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.