Skip to main content

Java Swing

Java Swing

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

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 Java Swing classes

The hierarchy of java swing API is given below.





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.


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

Example of Swing by Association inside constructor

We can also write all the codes of creating JFrame, JButton and method call inside the java constructor.

File: Simple.java

  1. import javax.swing.*;  
  2. public class Simple {  
  3. JFrame f;  
  4. Simple(){  
  5. f=new JFrame();//creating instance of JFrame  
  6.           
  7. JButton b=new JButton("click");//creating instance of JButton  
  8. b.setBounds(130,100,10040);  
  9.           
  10. f.add(b);//adding button in JFrame  
  11.           
  12. f.setSize(400,500);//400 width and 500 height  
  13. f.setLayout(null);//using no layout managers  
  14. f.setVisible(true);//making the frame visible  
  15. }  
  16.   
  17. public static void main(String[] args) {  
  18. new Simple();  
  19. }  
  20. }  

The setBounds(int xaxis, int yaxis, int width, int height)is used in the above example that sets the position of the button.


Simple example of Swing by inheritance

We can also inherit the JFrame class, so there is no need to create the instance of JFrame class explicitly.

File: Simple2.java

  1. import javax.swing.*;  
  2. public class Simple2 extends JFrame{//inheriting JFrame  
  3. JFrame f;  
  4. Simple2(){  
  5. JButton b=new JButton("click");//create button  
  6. b.setBounds(130,100,10040);  
  7.           
  8. add(b);//adding button on frame  
  9. setSize(400,500);  
  10. setLayout(null);  
  11. setVisible(true);  
  12. }  
  13. public static void main(String[] args) {  
  14. new Simple2();  
  15. }}  
Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are following header files important to C++ pro