Swing and Advanced Swing Components
Swing and Advanced Swing Components
A JComboBox in Java Swing is a drop-down list that allows users to select one item from a predefined
set of options.
constructors:
Constructor Description
JComboBox() Creates an empty combo box.
JComboBox(Object[] items) Creates a combo box with an array of items.
Example:
import java.awt.FlowLayout;
import javax.swing.*;
public class JComboEx {
}
JButton
A JButton in Java Swing is a clickable button used to perform an ActionEvent when clicked.
constructors:
Constructor Description
JButton() Creates an empty button.
JButton(String text) Creates a button with the given text.
JButton(Icon icon) Creates a button with an icon.
JButton(String text, Icon icon) Creates a button with both text and an icon.
JButton methods:
Method Description
setText(String text) Sets the button text.
getText() Gets the button text.
setIcon(Icon icon) Sets an icon on the button.
addActionListener(ActionListener l) Adds a click event listener.
setEnabled(boolean b) Enables or disables the button.
setVisible(boolean b) Shows or hides the button.
Example:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JButtonEx extends JFrame implements ActionListener
{
JLabel l1;
JButtonEx()
{
setSize(400,400);
setVisible(true);
setLayout(new FlowLayout());
l1=new JLabel("Sample Text");
add(l1);
JButton b1=new JButton("OK");
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
l1.setText("You Have Clicked OK Button");
}
public static void main(String[] args)
{
JButtonEx ex1=new JButtonEx();
}
}
Output
A JRadioButton in Java Swing is a button that allows users to select only one option from a group.
Here’s a simple table of JRadioButton constructors:
Constructor Description
JRadioButton() Creates an unselected radio button with no
label.
JRadioButton(String text) Creates a radio button with the given label.
JRadioButton(String text, boolean Creates a radio button with a label and sets its
selected) initial state.
JRadioButton(Icon icon) Creates a radio button with an icon.
JRadioButton(Icon icon, boolean Creates a radio button with an icon and sets its
selected) initial state.
Method Description
setSelected(boolean b) Selects (true) or deselects (false) the radio button.
isSelected() Checks if the radio button is selected.
setText(String text) Sets the label of the radio button.
getText() Gets the label of the radio button.
setEnabled(boolean b) Enables or disables the radio button.
addActionListener(ActionListener
l) Adds an event listener for button clicks.
Example:
import javax.swing.*;
import java.awt.*;
public class CourseSelection {
public static void main(String[] args)
{
JFrame f1=new JFrame("CourseSelection");
f1.setSize(400,400);
f1.setLayout(new FlowLayout());
f1.setVisible(true);
JRadioButton python=new JRadioButton("Python");
JRadioButton jpr=new JRadioButton("Java");
JRadioButton php=new JRadioButton("PHP");
JRadioButton csharp=new JRadioButton("C#");
f1.add(python);
f1.add(jpr);
f1.add(php);
f1.add(csharp);
}
}
JCheckBox
A JCheckBox in Java Swing is a GUI component that allows users to select or deselect an option
independently.
JCheckBox constructors:
Constructor Description
JCheckBox() Creates an empty checkbox.
JCheckBox(String text) Creates a checkbox with a label.
JCheckBox(String text, boolean Creates a checkbox with a label and sets its
selected) initial state.
Here’s a simple table of common JCheckBox methods:
Method Description
setSelected(boolean b) Checks (true) or unchecks (false) the checkbox.
isSelected() Returns true if the checkbox is checked.
setText(String text) Sets the label of the checkbox.
getText() Gets the label of the checkbox.
setEnabled(boolean b) Enables or disables the checkbox.
addActionListener(ActionListener l) Adds an event listener for checkbox selection.
Example
import javax.swing.*;
import java.awt.*;
public class MulCourseSel {
Constructor Description
JTable() Creates an empty table.
JTable(Sring[][] data, Sring [] Creates a table with data and column
columnNames) names.
Program
import javax.swing.*;
import java.awt.*;
public class EmpTable
{
public static void main(String[] args)
{
JFrame f1=new JFrame("Emp data");
f1.setSize(400,400);
f1.setLayout(new FlowLayout());
f1.setVisible(true);
String[][] data= { {"101","Ajit","67000"},
{"102","Anita","167000"},
{ "103","Ankit","160000"} };
String [] colhead= {"ID","Name","Salary"};
JTable jt=new JTable(data,colhead);
JScrollPane jp=new JScrollPane(jt);
f1.add(jp);
}
}
Output
JScrollPane
A JScrollPane provides a scrollable view for large components like JTable, JTextArea, or JPanel.
Key Features of JScrollPane
✔ Enables scrolling for components that exceed the visible area.
✔ Works with JTable, JTextArea, JPanel, and more.
✔ Supports horizontal & vertical scrolling automatically.
JScrollPane constructors:
Constructor Description
JScrollPane() Creates an empty scroll pane.
JScrollPane(Component view) Creates a scroll pane for the given component
(e.g., JTable, JTextArea).
JScrollPane(Component view, int Creates a scroll pane with custom scrollbar
vsbPolicy, int hsbPolicy) policies.
Scrollbar Policies:
1. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
2. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
3. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
4. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
5. Here’s a simple table of common JScrollPane methods:
Method Description
setVerticalScrollBarPolicy(int Sets the vertical scrollbar behavior (AS_NEEDED,
policy)
ALWAYS, NEVER).
setHorizontalScrollBarPolicy(int Sets the horizontal scrollbar behavior.
policy)
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_N
EEDED);
import javax.swing.*;
import java.awt.*;
JToolTip
A JToolTip is a Swing component that provides a small pop-up message when Moving the mouse over
the specified component. It helps users understand the purpose of buttons, text fields, and other UI
elements.
Here’s a simple table of JToolTip constructors:
Constructor Description
JToolTip() Creates a default tooltip with no text.
Unlike other components, JToolTip doesn't have multiple constructors. Instead, we usually use
setToolTipText("text") on components like JButton, JLabel, etc.
Example
import javax.swing.*;
import java.awt.*;
public class JToolTipEx
{
public static void main(String[] args)
{
JFrame frame = new JFrame("JToolTip Example");
frame.setSize(300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JLabel pass=new JLabel("Password");
JPasswordField textPass=new JPasswordField(" ");
textPass.setToolTipText("Please Enter Your Password");
frame.add(pass);
frame.add(textPass);
}
}
JTree
A JTree is a Swing component used to display hierarchical data in a tree-like structure, such as file
directories or organization charts.
JTree constructors:
Constructor Description
JTree() Creates an empty tree.
JTree(Object[] value) Creates a tree with an array of root elements.
DefaultMutableTreeNode Class
The DefaultMutableTreeNode class is used to create nodes for a JTree. It allows hierarchical data
representation.
Example
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
frame.add(scrollPane);
frame.setVisible(true);
}
}