Java Swing
• 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 [Link] package provides classes for
java swing API such as
• JButton,
• JTextField,
• JTextArea,
• JRadioButton,
• JCheckbox,
• JMenu,
• JColorChooser
Hierarchy of Java Swing classes
Swing Components
• JButton
• JLabel
• JTextField
• JCheckBox
• JRadioButton
• JComboBox
• JList
• JTable
Swing Layout Managers
• What are layout managers?
• Types of layout managers in Swing:
– BorderLayout
– FlowLayout
– GridLayout
– GridBagLayout
– BoxLayout
– CardLayout
Examples
import [Link].*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame
JButton b=new JButton("click");//creating instance of JButton
[Link](130,100,100, 40);//x axis, y axis, width, height
[Link](b);//adding button in JFrame
[Link](400,500);//400 width and 500 height
[Link](null);//using no layout managers
[Link](true);//making the frame visible
}
}
Output
import [Link].*;
public class Simple {
JFrame f;
Simple(){
f=new JFrame();//creating instance of JFrame
JButton b=new JButton("click");//creating instance of JButton
[Link](130,100,100, 40);
[Link](b);//adding button in JFrame
[Link](400,500);//400 width and 500 height
[Link](null);//using no layout managers
[Link](true);//making the frame visible
}
public static void main(String[] args) {
new Simple();
}
}
import [Link].*;
public class Simple2 extends JFrame{//inheriting JFrame
JFrame f;
Simple2(){
JButton b=new JButton("click");//create button
[Link](130,100,100, 40);
add(b);//adding button on frame
setSize(400,500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new Simple2();
}}
Java JButton
The JButton class is used to create a labeled button that has platform independent implementation.
The application result in some action when the button is pushed. It inherits AbstractButton class.
public class JButton extends AbstractButton implements Accessible
Commonly used Constructors
Constructor Description
JButton() It creates a button with no text and icon.
JButton(String s) It creates a button with the specified text.
JButton(Icon i) It creates a button with the specified icon object.
Commonly used Methods of AbstractButton class
Methods Description
void setText(String s) It is used to set specified text on button
String getText() It is used to return the text of the button.
void setEnabled(boolean b) It is used to enable or disable the button.
void setIcon(Icon b) It is used to set the specified Icon on the button.
Icon getIcon() It is used to get the Icon of the button.
void setMnemonic(int a) It is used to set the mnemonic on the button.
void It is used to add the action listener to this object.
addActionListener(ActionList
ener a)
Java Button Example
import [Link].*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
JButton b=new JButton("Click Here");
[Link](50,100,95,30);
[Link](b);
[Link](400,400);
[Link](null);
[Link](true);
}
}
• Java JButton Example with ActionListener
• import [Link].*;
• import [Link].*;
• public class ButtonExample {
• public static void main(String[] args) {
• JFrame f=new JFrame("Button Example");
• final JTextField tf=new JTextField();
• [Link](50,50, 150,20);
• JButton b=new JButton("Click Here");
• [Link](50,100,95,30);
• [Link](new ActionListener(){
• public void actionPerformed(ActionEvent e){
• [Link]("Welcome to Javatpoint.");
• }
• });
• [Link](b);[Link](tf);
• [Link](400,400);
• [Link](null);
• [Link](true);
• }
• }
import [Link].*;
public class ButtonExample{
ButtonExample(){
JFrame f=new JFrame("Button Example");
JButton b=new JButton(new ImageIcon("D:\\
[Link]"));
[Link](100,100,100, 40);
[Link](b);
[Link](300,400);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_C
LOSE);
}
public static void main(String[] args) {
new ButtonExample();
}
}