Java Swing
Java Swing
Swing Framework contains a set of classes that provides more powerful and flexible GUI
components than those of AWT. Swing 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.
JFC is an abbreviation for Java Foundation classes, which encompass a group of features for
building Graphical User Interfaces(GUI) and adding rich graphical functionalities and
interactivity to Java applications. Java Swing is a part of Java Foundation Classes (JFC).
Features of JFC
JPanel : JPanel is Swing's version of AWT class Panel and uses the same default layout,
FlowLayout. JPanel is descended directly from JComponent.
JFrame : JFrame is Swing's version of Frame and is descended directly from Frame class. The
component which is added to the Frame, is refered as its Content.
JWindow : This is Swing's version of Window and has descended directly from Window class.
Like Window it uses BorderLayout by default.
JLabel : JLabel has descended from JComponent, and is used to create text labels.
JButton : JButton class provides the functioning of push button. JButton allows an icon, string
or both associated with a button.
1. Import the javax.swing and java.awt package to use the classes and methods of Swing.
2. While creating a frame (either by instantiating or extending Frame class), following two
attributes are must for visibility of the frame:
3. setSize(int width, int height);
setVisible(true);
4. When you create objects of other components like Buttons, TextFields, etc. Then you
need to add it to the frame by using the method - add(Component's Object);
5. You can add the following method also for resizing the frame - setResizable(true);
JButton
JButton class provides functionality of a button. JButton class has three constuctors,
JButton(Icon ic)
JButton(String str)
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.
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();
}
}
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)
JCheckBox
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();
}
}
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();
}
}
StColor(){
}
//The below method is called whenever a button is clicked
@Override
public void actionPerformed(ActionEvent e) {
class Test {
public static void main(String[] args) {
StColor o = new StColor();
}
}
Ouput:
Apart from these classes java.lang.Class is another very important class used in Reflection
API.
Uses of Reflection
Developing IDE
Debugging and Test tools
Loading drivers and providing dynamic information
Disadvantages of Reflection
Low performance
Security risk
Violation of Oops concept
Java java.lang.Class class
Class is a final class in java.lang package which extends Object class. Instance of this class
represents classes and interfaces in a running Java application. It is used to analyze and change
dynamic behaviour of a class at runtime.
This class defines several methods using which we can get information about methods,
constructors, modifiers and members of a class at runtime.
forName()
This method takes fully qualified name of classes or interface as its argument and returns
instance of the class assocaited with it. Syntax
Student
getConstructors() method returns array of Constructors object that represent all the public
constructors of the invoking object. Remember, this method only returns public constructors. If
you want to see all the declared constructors of a class then use getDeclaredConstructors().
Following is the general syntax of both,
Constructor< ?>[] getConstructors();
Constructor< ?>[] getDeclaredConstructors();
class Test
{
public static void main(String args[])
{
try
{
Class c = Class.forName("Student");
Constructor< Student>[] ct = c.getConstructors();
for(int i=0; i< ct.length; i++)
{ System.out.println(ct[i]); }
Constructor< Student>[] cdt = c.getDeclaredConstructors();
for(int i=0;i< cdt.length;i++)
{ System.out.println(cdt[i]);}
}
catch(Exception e)
{ e.printStackTrace();}
}
}
getMethods() method returns array of Method object that reflect all the public method of
invoking object. getDeclaredMethods() returns only the declared methods of the invoking
class object. Syntax for both is following,
class Test
{
public static void main(String args[])
{
try
{
Class c = Class.forName("Student");
Method md[] = c.getDeclaredMethods();
for(int i=0; i< md.length; i++ )
{ System.out.println(md[i]); }
}
catch(Exception e)
{ e.printStackTrace();}
}
}
getFields() returns an array containing Field objects reflecting all the accessible public
members of the class or interface represented by this Class object. getDeclaredFields()
returns array of Field objects reflecting all the fields declared by the class or interface
represented by this Class object.
class Test
{
public static void main(String args[])
{
try
{
Class c = Class.forName("Student");
Field ff[] = c.getFields();
for(int i=0; i< ff.length; i++)
{ System.out.println(ff[i]); }
Field f[] = c.getDeclaredFields();
for(int i=0;i< f.length; i++)
{ System.out.println(f[i]); }
}
catch(Exception e)
{ e.printStackTrace();}
}
}