Swing Notes: Java Swing Is A Part of Java Foundation Classes (JFC) That Is Used To Create Window-Based

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Swing Notes

Swing- architecture,components of swing- JLabel,JButton, JCheckBox, JRadioButton,JList,


JComboBox, JTextField, JTextArea, JPanel, JFrame

Java Swing is a part of Java Foundation Classes (JFC) that 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


Components of Swing

Java JLabel

The object of 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 application but a user cannot edit it
directly. It inherits JComponent class.

Eg:

import javax.swing.*;  

class LabelExample  

{  

public static void main(String args[])  
    {  

    JFrame f= new JFrame("Label Example");  

    JLabel l1,l2;  

    l1=new JLabel("First Label.");  

    l1.setBounds(50,50, 100,30);  

    l2=new JLabel("Second Label.");  

    l2.setBounds(50,100, 100,30);  

    f.add(l1); f.add(l2);  

    f.setSize(300,300);  

    f.setLayout(null);  

    f.setVisible(true);  

    }  

    }  

Output

Java JButton

The JButton class is used to create a labeled button that has platform independent
implementation. The application result in some action when the button is pushed. It inherits
AbstractButton class.
import javax.swing.*;    

public class ButtonExample {  

public static void main(String[] args) {  

    JFrame f=new JFrame("Button Example");  

    JButton b=new JButton("Click Here");  

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

    f.add(b);  

    f.setSize(400,400);  

    f.setLayout(null);  

    f.setVisible(true);   

}  

}  

Output

Java JTextField

The object of a JTextField class is a text component that allows the editing of a single line text. It
inherits JTextComponent class.
import javax.swing.*;  

class TextFieldExample  

{  

public static void main(String args[])  

    {  

    JFrame f= new JFrame("TextField Example");  

    JTextField t1,t2;  

    t1=new JTextField("Welcome to Javatpoint.");  

    t1.setBounds(50,100, 200,30);  

    t2=new JTextField("AWT Tutorial");  

    t2.setBounds(50,150, 200,30);  

    f.add(t1); f.add(t2);  

    f.setSize(400,400);  

    f.setLayout(null);  

    f.setVisible(true);  

    }  

    }  
Java JTextArea

The object of a JTextArea class is a multi line region that displays text. It allows the editing of
multiple line text. It inherits JTextComponent class

import javax.swing.*;  

public class TextAreaExample  

{  

     TextAreaExample(){  

        JFrame f= new JFrame();  

        JTextArea area=new JTextArea("Welcome to javatpoint");  

        area.setBounds(10,30, 200,200);  

        f.add(area);  

        f.setSize(300,300);  

        f.setLayout(null);  

        f.setVisible(true);  

     }  

public static void main(String args[])  

    {  

   new TextAreaExample();  

    }}  

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 ".It
inherits JToggleButton class.

import javax.swing.*;  

public class CheckBoxExample  

{  

     CheckBoxExample(){  

        JFrame f= new JFrame("CheckBox Example");  

        JCheckBox checkBox1 = new JCheckBox("C++");  

        checkBox1.setBounds(100,100, 50,50);  

        JCheckBox checkBox2 = new JCheckBox("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 CheckBoxExample();  

    }}  
Java JRadioButton

The 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 or quiz.

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

import javax.swing.*;    

public class RadioButtonExample {    

JFrame f;    

RadioButtonExample(){    

f=new JFrame();     

JRadioButton r1=new JRadioButton("A) Male");    

JRadioButton r2=new JRadioButton("B) Female");    

r1.setBounds(75,50,100,30);    

r2.setBounds(75,100,100,30);    

ButtonGroup bg=new ButtonGroup();    
bg.add(r1);bg.add(r2);    

f.add(r1);f.add(r2);      

f.setSize(300,300);    

f.setLayout(null);    

f.setVisible(true);    

}    

public static void main(String[] args) {    

    new RadioButtonExample();    

}    

}    

Java JComboBox

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. It inherits JComponent class.

import javax.swing.*;    

public class ComboBoxExample {    
JFrame f;    

ComboBoxExample(){    

    f=new JFrame("ComboBox Example");    

    String country[]={"India","Aus","U.S.A","England","Newzealand"};        

    JComboBox cb=new JComboBox(country);    

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

    f.add(cb);        

    f.setLayout(null);    

    f.setSize(400,500);    

    f.setVisible(true);         

}    

public static void main(String[] args) {    

    new ComboBoxExample();         

}    

}   
Java JList

The 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 either one item or multiple items. It inherits JComponent class.

import javax.swing.*;  

public class ListExample  

{  

     ListExample(){  

        JFrame f= new JFrame();  

        DefaultListModel<String> l1 = new DefaultListModel<>();  

          l1.addElement("Item1");  

          l1.addElement("Item2");  

          l1.addElement("Item3");  

          l1.addElement("Item4");  

          JList<String> list = new JList<>(l1);  

          list.setBounds(100,100, 75,75);  

          f.add(list);  

          f.setSize(400,400);  

          f.setLayout(null);  

          f.setVisible(true);  

     }  

public static void main(String args[])  

    {  

   new ListExample();  

    }}  
Java JFrame

The javax.swing.JFrame class is a type of container which inherits the java.awt.Frame class.
JFrame works like the main window where components like labels, buttons, textfields are added
to create a GUI.

Unlike Frame, JFrame has the option to hide or close the window with the help of
setDefaultCloseOperation(int) method.

Java JPanel

The JPanel is a simplest container class. It provides space in which an application can attach any
other component. It inherits the JComponents class.

It doesn't have title bar.

You might also like