!-- Global site tag (gtag.js) - Google Analytics --> swing tutorial - programmingworld

Breaking

swing tutorial


    Swing:-

AWT API, introduced in JDK 1.0, does not support any GUI component for table, tree, pop up menu, progress bar, color chooser etc. A much more comprehensive set of graphics libraries that enhances the AWT was introduced in JDK 1.1 and was known as Swing API. In fact, Swing API includes all AWT components with better look and feel. Specifically, swing has the following features:

More Components-

 Swing provides a rich set of advanced controls for a table, tree, tabbed pane, slider, color chooser etc.   

Light Weight-

Swing is lightweight toolkit which does use underlying operating system calls. Instead, swing components are rendered using pure JAVA code.

Pluggable look and feel-

The look and feel properties of swing components can be changed at runtime.

Highly Customizable-

The appearance of swing components is independent of internal representation, and thereby highly customizable.

swing container class:-

A container class is used to creating a graphic user interface(GUI). A Container class is contains the other component/class. Container is three types.

  • Frame
  • Panel
  • Dialog 
 Frame:- Frame is part of the window. It is functionally works like windows. It is not a pure container.
Panel:- Panel is not working as a window. The Panel is a pure container.
Dialog:- Dialog is like a pop-up window. It is used when we want to display a message.

ADDING Swing COMPONENTS-

Swing Examples- 
1.   JTextField- 
 Import java.awt.*;
 Import javax.swing.*;
Public class JTextFieldDemo
{
Public static void main(String s[])
{
JFrame f =new JFrame(“JTextField Demo”);
Container c=f.getContentPane();
f.setSize(200,80);
c.setLayout(new FlowLayout());
         c.add(new JLabel("name"));
         c.add(new JTextField(10));
         f.setVisible(true);
}}
output

Java JTextField Example with ActionListener 

import javax.swing.*; 

import java.awt.event.*;

public class TextExample implements ActionListener{ 

JTextField t1,t2,t3; 

JButton a,b; 

TextExample () { 

JFrame f= new JFrame(); 

t1=new JTextField(); 

t1.setBounds(70,50,150,30); 

t2=new JTextField(); 

t2.setBounds(70,100,150,30); 

t3=new JTextField(); 

t3.setBounds(70,150,150,30); 

t3.setEditable(false);  

a=new JButton("+"); 

a.setBounds(70,200,50,50); 

b=new JButton("-"); 

b.setBounds(120,200,50,50); 

a.addActionListener(this); 

b.addActionListener(this); 

f.add(t1);f.add(t2);f.add(t3);f.add(b1);f.add(b2); 

f.setSize(300,300); 

f.setLayout (null); 

f.setVisible(true); 

 }        

public void actionPerformed(ActionEvent e) { 

String s1=t1.getText(); 

String s2=t2.getText(); 

int a1=Integer.parseInt(s1); 

int b1=Integer.parseInt(s2); 

int c=0; 

if(e.getSource()==a){ 

c=a1+b1;  

 }else if(e.getSource()==b){ 

 c=a1-b1; 

 } 

 String result=String.valueOf(c); 

 t3.setText(result); 

  } 

 public static void main(String[] args) { 

 new TextExample(); 

                   } } 

2.   JPasswordField-

import java.awt.*;
import javax.swing.*;
public class JPasswordFieldDemo
{
public static void main(String s[])
{
JFrame f =new JFrame("JPasswordField Demo");
Container c=f.getContentPane();
f.setSize(200,200);
c.setLayout(new FlowLayout());
c.add(new JLabel("password"));
 final JPasswordField pf=new JPasswordField(10);
 c.add(pf);
 f.setVisible(true);

}}
output



Methods of Component class in swing

 





Method
Description
public void setSize(int width x,int height y)
set component size.
public void add(Component e)
add another component to a component.
public void setLayout(Layout lm)
sets the layout manager for a component.
public void setVisible(arithmetic a)
Use for sets the component visibility.



Difference between AWT and Swing

There are some differences between java awt and swing that are given below.


AWT
Swing

 components are platform-dependent.
Java swing components are platform-independent.

AWT components are heavyweight.

 lightweight components are available.

AWT did not support pluggable look.

It supports the pluggable look.

AWT provides less components than Swing.

Swing provides more components such as tables, lists, scroll panes, color chooser, tabbed pane etc.

AWT doesn't follow the Model View Controller(MVC) where model represents data, the view represents presentation and controller acts as an interface between model and view.
Swing follows MVC.