SWING

Swing Framework contains a set of classes that provides more powerful and flexible GUI components than those of AWTSwing provides the look and feel of modern Java GUI. Swing library is an official Java GUI tool kit released by Sun Microsystems. It is used to create graphical user interface with Java.

Main Features of Swing Toolkit

  1. Platform Independent
  2. Customizable
  3. Extensible
  4. Configurable
  5. Lightweight

Hierarchy of Java Swing classes

The hierarchy of java swing API is given below.
hierarchy of javax swing


Simple Java Swing Example

Let's see a simple swing example where we are creating one button and adding it on the JFrame object inside the main() method.
File: FirstSwingExample.java
  1. import javax.swing.*;  
  2. public class FirstSwingExample {  
  3. public static void main(String[] args) {  
  4. JFrame f=new JFrame();//creating instance of JFrame  
  5.           
  6. JButton b=new JButton("click");//creating instance of JButton  
  7. b.setBounds(130,100,10040);//x axis, y axis, width, height  
  8.           
  9. f.add(b);//adding button in JFrame  
  10.           
  11. f.setSize(400,500);//400 width and 500 height  
  12. f.setLayout(null);//using no layout managers  
  13. f.setVisible(true);//making the frame visible  
  14. }  
  15. }  
simple example of java swing

Swing Component

Swing Framework contains a large set of components which provide rich functionalities and allow high level of customization. All these components are lightweight components. They all are derived from JComponentclass. It supports the pluggable look and feel.

JButton

JButton class provides functionality of a button. JButton class has three constuctors,
JButton(Icon ic)

JButton(String str)

JButton(String str, Icon ic)
It allows a button to be created using icon, a string or both. JButton supports ActionEvent. When a button is pressed an ActionEvent is generated.

Example using JButton

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class testswing extends JFrame 
{
 
 testswing()
 {
  JButton bt1 = new JButton("Yes");  //Creating a Yes Button. 
  JButton bt2 = new JButton("No");  //Creating a No Button.  
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)     //setting close operation. 
  setLayout(new FlowLayout());  //setting layout using FlowLayout object  
  setSize(400, 400);   //setting size of Jframe 
  add(bt1);  //adding Yes button to frame. 
  add(bt2);  //adding No button to frame. 
  
  setVisible(true);
 }
 public static void main(String[] args)
 {
  new testswing();
 }
}
swing button example

JTextField

JTextField is used for taking input of single line of text. It is most widely used text component. It has three constructors,
JTextField(int cols)
JTextField(String str, int cols)
JTextField(String str)
cols represent the number of columns in text field.

Example using JTextField

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MyTextField extends JFrame 
{
 public MyTextField() 
 {
  JTextField jtf = new JTextField(20); //creating JTextField. 
  add(jtf);    //adding JTextField to frame. 
  setLayout(new FlowLayout());
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setSize(400, 400);
  setVisible(true);
 }
 public static void main(String[] args) 
 {
  new MyTextField();
 }
}
jtextfield example

JCheckBox

JCheckBox class is used to create checkboxes in frame. Following is constructor for JCheckBox,
JCheckBox(String str)

Example using JCheckBox

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Test extends JFrame  
{
 public Test() 
 {
  JCheckBox jcb = new JCheckBox("yes");   //creating JCheckBox. 
  add(jcb);      //adding JCheckBox to frame. 
  jcb = new JCheckBox("no");    //creating JCheckBox. 
  add(jcb);      //adding JCheckBox to frame. 
  jcb = new JCheckBox("maybe");    //creating JCheckBox.  
  add(jcb);      //adding JCheckBox to frame. 
  setLayout(new FlowLayout());
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setSize(400, 400);
  setVisible(true);
 }
 public static void main(String[] args) 
 {
  new Test();
 }
}
jcheckbox example

JRadioButton

Radio button is a group of related button in which only one can be selected. JRadioButton class is used to create a radio button in Frames. Following is the constructor for JRadioButton,
JRadioButton(String str)

Example using JRadioButton

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Test extends JFrame 
{
 public Test() 
 {
  JRadioButton jcb = new JRadioButton("A"); //creating JRadioButton.  
  add(jcb);     //adding JRadioButton to frame. 
  jcb = new JRadioButton("B");   //creating JRadioButton. 
  add(jcb);     //adding JRadioButton to frame. 
  jcb = new JRadioButton("C");   //creating JRadioButton.  
  add(jcb);     //adding JRadioButton to frame. 
  jcb = new JRadioButton("none");
  add(jcb);
  setLayout(new FlowLayout());
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setSize(400, 400);
  setVisible(true);
 }
 public static void main(String[] args)
 {
  new Test();
 }
}
jradiobutton example

JComboBox

Combo box is a combination of text fields and drop-down list.JComboBox component is used to create a combo box in Swing. Following is the constructor for JComboBox,
JComboBox(String arr[])

Example using JComboBox

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Test extends JFrame 
{
 String name[] = {"Abhi","Adam","Alex","Ashkay"};  //list of name. 
 public Test() 
 {
  JComboBox jc = new JComboBox(name); //initialzing combo box with list of name. 
  add(jc);    //adding JComboBox to frame. 
  setLayout(new FlowLayout());
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setSize(400, 400);
  setVisible(true);
 }
 public static void main(String[] args)
 {
  new Test();
 }
}
jcombobox example








2 comments:

  1. I liked this article very much. The content is very good. Keep posting.
    Visit us: Java Training
    Visit us: Java Course

    ReplyDelete
  2. Thanks for your information, it was really very helpful, Very Informative and well explained.
    best mirrorless camera
    Best cinema cameras
    Best travel cameras

    ReplyDelete