Monday, 29 September 2014

Java Swings


Java Swing tutorial 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 etc.
Difference between AWT and Swing 
There are many differences between java awt and swing that are given below.
No.Java AWTJava 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, scrollpanes, colorchooser, tabbedpane etc.
5)AWT doesn't follows MVC(Model View Controller) where model represents data, view represents presentation and controller acts as an interface between model and view.Swing follows MVC.

What is JFC

The Java Foundation Classes (JFC) are a set of GUI components which simplify the development of desktop applications.

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

http://www.javatpoint.com/images/swinghierarchy.jpg

Commonly used Methods of Component class

The methods of Component class are widely used in java swing that are given below.
MethodDescription
public void add(Component c)add a component on another component.
public void setSize(int width,int height)sets size of the component.
public void setLayout(LayoutManager m)sets the layout manager for the component.
public void setVisible(boolean b)sets the visibility of the component. It is by default false.

Java Swing Examples

There are two ways to create a frame:
  • By creating the object of Frame class (association)
  • By extending Frame class (inheritance)
We can write the code of swing inside the main(), constructor or any other method.

Example of Swing by Association inside constructor

import javax.swing.*;
public class Sample{
    JFrame frame;
    Simple(){
    frame = new JFrame ();//creating new instance of JFrame
    JButton button = new JButton("Click");
    button.setBounds(130,100,100,40);//setBounds(int xaxis, int yaxis, int width, int height)
     frame.add(button); //adding button to JFrame
     frame.setSize(400,500);//setSize(int width,intheight)
     f.setLayout(null);//using no Layout mangers
     f.setVisible(true);
    }
   public static void main(String[] args){
          new Sample();
   }
}

Simple example of Swing by inheritance

import javax.swing.*;
public class Simple2 extends JFrame{//inheriting JFrame
    JFrame frame;
    Simple2(){
    JButton button = new JButton("click");//create button
    button.setBounds(130,100,100, 40);
    add(button);//adding button on frame
    setSize(400,500);
    setLayout(null);
    setVisible(true);
    }
public static void main(String[] args) { 
    new Simple2();
    }
}

 

No comments:

Simplest way to display Server Time in JSP using js

THE BELOW CODE WORKS ONLY IN JSP : <html> <body > <table width="95%" border="1" cellpadding="1...