[go: up one dir, main page]

0% found this document useful (0 votes)
14 views10 pages

Swing and Advanced Swing Components

The document provides an overview of various Java Swing components including JComboBox, JButton, JRadioButton, JCheckBox, JTable, JScrollPane, JToolTip, and JTree, detailing their constructors, methods, and examples of usage. Each component is described with its purpose and how to implement it in a Java application. Additionally, it highlights key features and functionalities of these components for creating graphical user interfaces.

Uploaded by

parashyapillu143
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)
14 views10 pages

Swing and Advanced Swing Components

The document provides an overview of various Java Swing components including JComboBox, JButton, JRadioButton, JCheckBox, JTable, JScrollPane, JToolTip, and JTree, detailing their constructors, methods, and examples of usage. Each component is described with its purpose and how to implement it in a Java application. Additionally, it highlights key features and functionalities of these components for creating graphical user interfaces.

Uploaded by

parashyapillu143
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/ 10

JComboBox:

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 {

public static void main(String[] args)


{
JFrame frame =new JFrame();
frame.setSize(400,400);
frame.setVisible(true);
frame.setLayout(new FlowLayout());
String city[]= {"Pune","Nasik","Kolhapur","Sangamner","Sambhajinagar"};
JComboBox cb=new JComboBox (city);
frame.add(cb);
}

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

Here’s a simple table of common JRadioButton methods:

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 {

public static void main(String[] args)


{
JFrame f1=new JFrame("CourseSelection");
f1.setSize(400,400);
f1.setLayout(new FlowLayout());
f1.setVisible(true);
JCheckBox python=new JCheckBox("Python",true);
JCheckBox jpr=new JCheckBox("Java");
JCheckBox php=new JCheckBox("PHP",true);
JCheckBox csharp=new JCheckBox ("C#");
f1.add(python);
f1.add(jpr);
f1.add(php);
f1.add(csharp);
}
}
OutPut
Advanced Swing Components
JTable
A JTable is a Swing component used to display data in a tabular format with rows and columns.
Key Features of JTable
✔ Displays structured data in rows & columns.
✔ Supports scrolling using JScrollPane

Here’s a simple table of JTable constructors:

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)

Example Scrollbar Policies:


scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_N

EEDED);

import javax.swing.*;
import java.awt.*;

public class JScrollPaneExample {


public static void main(String[] args) {
JFrame frame = new JFrame("JScrollPane Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
// Create a JTextArea
JTextArea textArea = new JTextArea(5, 20);
textArea.setText("This is a long text inside a JTextArea...\nKeep typing to see the scroll effect!");
// Add JTextArea to JScrollPane
JScrollPane scrollPane = new JScrollPane(textArea);
// Add JScrollPane to frame
frame.add(scrollPane);
frame.setVisible(true);
}
Output

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.

Key Features of DefaultMutableTreeNode

✔ Stores hierarchical data for JTree.


✔ Supports adding and removing child nodes using .add() and .remove().

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;

public class DefaultMutableTreeNodeExample {


public static void main(String[] args) {
JFrame frame = new JFrame("JTree Example");
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Creating the root node


DefaultMutableTreeNode root = new DefaultMutableTreeNode("Fruits");

// Creating child nodes


DefaultMutableTreeNode apple = new DefaultMutableTreeNode("Apple");
DefaultMutableTreeNode banana = new DefaultMutableTreeNode("Banana");
DefaultMutableTreeNode mango = new DefaultMutableTreeNode("Mango");

// Adding child nodes to root


root.add(apple);
root.add(banana);
root.add(mango);

// Creating a JTree with the root node


JTree tree = new JTree(root);
JScrollPane scrollPane = new JScrollPane(tree);

frame.add(scrollPane);
frame.setVisible(true);
}
}

You might also like