0% found this document useful (0 votes)
3 views

Swing

The document provides an overview of various Swing components in Java, including JButton, JLabel, JTextField, JTextArea, JRadioButton, JComboBox, JTable, JProgressBar, JToolTip, JTabbedPane, JScrollBar, JCheckBox, JTree, and JFrame. Each component is described with its purpose, usage, and a sample code snippet demonstrating its implementation. Additionally, it briefly mentions the difference between AWT and Swing.

Uploaded by

atharvanichat7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Swing

The document provides an overview of various Swing components in Java, including JButton, JLabel, JTextField, JTextArea, JRadioButton, JComboBox, JTable, JProgressBar, JToolTip, JTabbedPane, JScrollBar, JCheckBox, JTree, and JFrame. Each component is described with its purpose, usage, and a sample code snippet demonstrating its implementation. Additionally, it briefly mentions the difference between AWT and Swing.

Uploaded by

atharvanichat7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Swing

JButton
 The JButton class is used to create a labeled button
 The application result in some action when the button is pushed.
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);
}
}
JLabel

 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.

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);
}
}
JTextField
 JTextField is a Swing component in Java that allows users to input
single-line text.
 It is commonly used in GUI (Graphical User Interface)
applications to accept textual input from users.
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);
}
}
JTextArea

 The object of a JTextArea class is a multi-line region that displays


text.
 It allows the editing of multiple-line text.
 An editable and showing multi-line text component in Java is
represented by the JTextArea class, which is a component of the
javax.swing package.
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();
}}
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();
}
}
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.

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();
}
}
JTable
The JTable class is used to display data in tabular form. It is composed of rows and
columns.

import javax.swing.*;
public class TableExample {
JFrame f;
TableExample(){
f=new JFrame();
String data[][]={ {"101","Amit","670000"},
{"102","Jai","780000"},
{"101","Sachin","700000"}};
String column[]={"ID","NAME","SALARY"};
JTable jt=new JTable(data,column);
jt.setBounds(30,40,200,300);
JScrollPane sp=new JScrollPane(jt);
f.add(sp);
f.setSize(300,400);
f.setVisible(true);
}
public static void main(String[] args) {
new TableExample();
}

JProgressBar
 The JProgressBar class is used to display the progress of the task.
 It inherits JComponent class.
import javax.swing.*;

public class ProgressBarExample extends JFrame{

JProgressBar jb;

int i=0,num=0;

ProgressBarExample(){

jb=new JProgressBar(0,2000);

jb.setBounds(40,40,160,30);

jb.setValue(0);

jb.setStringPainted(true);

add(jb);

setSize(250,150);

setLayout(null);

}
public void iterate(){

while(i<=2000){

jb.setValue(i);

i=i+20;

try{Thread.sleep(150);}catch(Exception e){}

public static void main(String[] args) {

ProgressBarExample m=new ProgressBarExample();

m.setVisible(true);

m.iterate();

}
JToolTip

setToolTipText() method is used to set up a tool tip for the component.

For example, to add tool tip to PasswordField, you need to add only one line of code:

field.setToolTipText("Enter your Password");

Simple ToolTip Example


import javax.swing.*;

public class ToolTipExample {

public static void main(String[] args) {

JFrame f=new JFrame("Password Field Example");

//Creating PasswordField and label

JPasswordField value = new JPasswordField();

value.setBounds(100,100,100,30);

value.setToolTipText("Enter your Password");

JLabel l1=new JLabel("Password:");

l1.setBounds(20,100, 80,30);

//Adding components to frame

f.add(value); f.add(l1);

f.setSize(300,300);

f.setLayout(null);

f.setVisible(true);

}
JTabbedPane
The JTabbedPane class is used to switch between a group of components by clicking on
a tab with a given title or icon. It inherits JComponent class.

import javax.swing.*;

public class TabbedPaneExample {

JFrame f;

TabbedPaneExample(){

f=new JFrame();

JTextArea ta=new JTextArea(200,200);

JPanel p1=new JPanel();

p1.add(ta);

JPanel p2=new JPanel();

JPanel p3=new JPanel();

JTabbedPane tp=new JTabbedPane();

tp.setBounds(50,50,200,200);
tp.add("main",p1);

tp.add("visit",p2);

tp.add("help",p3);

f.add(tp);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

public static void main(String[] args) {

new TabbedPaneExample();

}}
JScrollBar
 The object of JScrollbar class is used to add horizontal and vertical scrollbar.
 It is an implementation of a scrollbar. It inherits JComponent class.

import javax.swing.*;

class ScrollBarExample

ScrollBarExample(){

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

JScrollBar s=new JScrollBar();

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();

}}

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 ".

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();

}}
JTree
 The JTree class is used to display the tree structured data or hierarchical data.
 JTree is a complex component. It has a 'root node' at the top most which is a
parent for all nodes in the tree.
 It inherits JComponent class.

import javax.swing.*;

import javax.swing.tree.DefaultMutableTreeNode;

public class TreeExample

JFrame f;

TreeExample(){

f=new JFrame();

DefaultMutableTreeNode style=new DefaultMutableTreeNode("Style");

DefaultMutableTreeNode color=new DefaultMutableTreeNode("color");

DefaultMutableTreeNode font=new DefaultMutableTreeNode("font");

style.add(color);

style.add(font);

DefaultMutableTreeNode red=new DefaultMutableTreeNode("red");


DefaultMutableTreeNode blue=new DefaultMutableTreeNode("blue");

DefaultMutableTreeNode black=new DefaultMutableTreeNode("black");

DefaultMutableTreeNode green=new DefaultMutableTreeNode("green");

color.add(red); color.add(blue); color.add(black); color.add(green);

JTree jt=new JTree(style);

f.add(jt);

f.setSize(200,200);

f.setVisible(true);

public static void main(String[] args) {

new TreeExample();

}}
JFrame
 Thе Java JFramе is an essential componеnt of Java Swing,

 JFrame in Java is a class that allows you to crеatе and manage components in frame
window

 It sеrvеs as thе main window for GUI-basеd Java applications and providеs a platform-
indеpеndеnt way to crеatе graphical usеr intеrfacеs

 In Java JFrame is a part of javax.swing package.

Constructor of Java JFrame

Constructor Description

This is the default constructor for JFrame. It


JFrame()
creates a new frame with no title

This constructor creates a new frame with the


JFrame(String title)
specified title.

Methods of Java JFrame

Methods Description

setTitle(String title) Sets the title of the JFrame.

setSize(int width, int height) Sets the size of the JFrame.

Sets the visibility of the JFrame. Pass true


setVisible(boolean b)
to make it visible and false to hide it.

Sets the layout manager for the JFrame,


setLayout(LayoutManager manager) which controls how components are
arranged within the frame.

add(Component comp) Adds a Swing component to the JFrame.

remove(Component comp) Removes a component from the JFrame.


Difference Between AWT and Swing

You might also like