Swing
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
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
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
import javax.swing.*;
public class ComboBoxExample {
JFrame f;
ComboBoxExample(){
f=new JFrame("ComboBox Example");
String country[]={"India","Aus","U.S.A","England","Newzealand"};
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.*;
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){}
m.setVisible(true);
m.iterate();
}
JToolTip
For example, to add tool tip to PasswordField, you need to add only one line of code:
value.setBounds(100,100,100,30);
l1.setBounds(20,100, 80,30);
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.*;
JFrame f;
TabbedPaneExample(){
f=new JFrame();
p1.add(ta);
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);
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(){
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.*;
{
CheckBoxExample(){
checkBox1.setBounds(100,100, 50,50);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
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;
JFrame f;
TreeExample(){
f=new JFrame();
style.add(color);
style.add(font);
f.add(jt);
f.setSize(200,200);
f.setVisible(true);
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
Constructor Description
Methods Description