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

Java study Material Unit V April - May 2025

The document provides a comprehensive overview of Java Swing components, including their hierarchy and specific classes such as JFrame, JDialog, JPanel, JButton, JToggleButton, JCheckBox, JRadioButton, JLabel, JTextField, JTextArea, and JList. It details the purpose, constructors, and usage examples for each component, highlighting their roles in creating graphical user interfaces. Additionally, it categorizes Swing containers into top-level, general-purpose, and special-purpose containers.

Uploaded by

john8012karthik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java study Material Unit V April - May 2025

The document provides a comprehensive overview of Java Swing components, including their hierarchy and specific classes such as JFrame, JDialog, JPanel, JButton, JToggleButton, JCheckBox, JRadioButton, JLabel, JTextField, JTextArea, and JList. It details the purpose, constructors, and usage examples for each component, highlighting their roles in creating graphical user interfaces. Additionally, it categorizes Swing containers into top-level, general-purpose, and special-purpose containers.

Uploaded by

john8012karthik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

JAVA STUDY MATERIAL 2025

UNIT V

1. The Swing Component hierarchy

Class Description

Component An abstract base class that defines any object that can be displayed.

Container An abstract class that defines any component that can contain other components.

Window The AWT class that defines a window without a title bar or border.

Frame The AWT class that defines a window with a title bar and border.

JFrame The Swing class that defines a window with a title bar and border.

JComponent A base class for Swing components


components such as JPanel, JButton, JLabel, and JTextField.

JPanel The Swing class that defines a panel, which is used to hold other components.

JLabel The Swing class that defines a label.

JTextField The Swing class that defines a text field.

JButton The Swing class that defines a button.


2. Explain in details about Java Swing Containers

Swing containers can be classified into three main categories:


• Top-level containers:
JFrame, JWindow, and JDialog
• General-purpose containers:
JPanel, JScrollPane,JToolBar,JSplitPane, and JTabbedPane
• Special-purpose containers:
JInternalFrame and JLayeredPane

Top-Level Containers

Swing components can be broadly classified as:


• Buttons
• Text components
• Uneditable information display components
• Menus
• Formatted display components
• Other basic controls
3. Write Short note on java swing JFRAME
• JFrame works like the main window where components like labels, buttons, textfields are added to create
a GUI.

• JFrame has the option to hide or close the window with the help of setDefaultCloseOperation(int)
method.
There are two ways to create a frame:

 By creating the object of Frame class (Association)


 By extending Frame class (Inheritance)We can write the code of Swing inside the main(), constructor or any
other method.

JFrame object :

Let's see a simple swing example where we are creating one button and adding it on the JFrame object inside
the main() method.

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Jpanel;
public class JFrameExample {
public static void main(String s[]) {
JFrame frame = new JFrame("JFrame Example");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("JFrame By Example");
JButton button = new JButton();
button.setText("click");
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setSize(200, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

JFrame object
4. Write Short note on java swing JDialog

• The JDialog control represents a top level window with a border and a title used to take
some form of input from the user. It inherits the Dialog class.
• it doesn't have maximize and minimize buttons.

JDialog class declaration

The declaration for javax.swing.JDialog class.

public class JDialog extends Dialog implements WindowConstants, Accessible, RootPaneC


ontainer

Commonly used Constructors:

Constructor Description

JDialog() It is used to create a modeless dialog without a title


and without a specified Frame owner.

JDialog(Frame owner) It is used to create a modeless dialog with specified


Frame as its owner and an empty title.

JDialog(Frame owner, String title, It is used to create a dialog with the specified title,
boolean modal) owner Frame and modality.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DialogExample {
private static JDialog d;
DialogExample() {
JFrame f= new JFrame();
d = new JDialog(f , "Dialog Example", true);
d.setLayout( new FlowLayout() );
JButton b = new JButton ("OK");
b.addActionListener ( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
DialogExample.d.setVisible(false);
}
});
d.add( new JLabel ("Click button to continue."));
d.add(b);
d.setSize(300,300);
d.setVisible(true);
}
public static void main(String args[])
{
new DialogExample();
}
}
5. Write Short note on java swing JPanel
• The JPanel is the simplest container class. It inherits the JComponents class and provides a space for an
application to attach other components. It does not have a title bar.

• JPanel is a lightweight Java container that offers a place to organize and arrange other GUI (graphical user
interface) components. It is used as a building element in Swing apps to create intricate layouts. JPanel is
easily attached to other containers, such as JFrame or another JPanel, because it inherits from the
JComponent class.
Panel Class Declaration
public class JPanel extends JComponent implements Accessible

Commonly used Constructors:

Constructor Description

JPanel() It is used to create a new JPanel with a double


buffer and a flow layout.

JPanel(boolean isDoubleBuffered) It is used to create a new JPanel with


FlowLayout and the specified buffering
strategy.

JPanel(LayoutManager layout) It is used to create a new JPanel with the


specified layout manager.

JPanel(ComponentOrientation orientation, boolean Creates a new JPanel with the specified


isDoubleBuffered) component orientation and buffering strategy.

JPanel(LayoutManager layout, boolean isDoubleBuffered) Creates a new JPanel with the specified layout
manager and buffering strategy.

import java.awt.*;
import javax.swing.*;
public class PanelExample {
PanelExample()
{
JFrame f= new JFrame("Panel Example");
JPanel panel=new JPanel();
panel.setBounds(40,80,200,200);
panel.setBackground(Color.gray);
JButton b1=new JButton("Button 1");
b1.setBounds(50,100,80,30);
b1.setBackground(Color.yellow);
JButton b2=new JButton("Button 2");
b2.setBounds(100,100,80,30);
b2.setBackground(Color.green);
panel.add(b1); panel.add(b2);
f.add(panel);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new PanelExample();
}
}
6. Write Short note on java Swing 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.
Below is the declaration for javax.swing.JButton class.

public class JButton extends AbstractButton implements Accessible

Class JButton : Constructors:

Class JButton : Methods:

Example:

// Button with Action Listner


import java.awt.event.*;
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
final JTextField tf=new JTextField();
tf.setBounds(50,50, 150,20);
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
tf.setText("Welcome to IIT Kharagpur.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
7. Write Short note on java Swing JToggleButton
• JToggleButton is used to create toggle button, it is two-states button to switch on or off.

Nested Classes
Modifier and Type Class Description

protected class JToggleButton.AccessibleJToggleButton This class implements accessibility


support for the JToggleButton class.

static class JToggleButton.ToggleButtonModel The ToggleButton model

Constructor

Constructor Description

JToggleButton() It creates an initially unselected toggle button without


setting the text or image.

JToggleButton(Action a) It creates a toggle button where properties are taken


from the Action supplied.

JToggleButton(Icon icon) It creates an initially unselected toggle button with the


specified image but no text.

JToggleButton(Icon icon, boolean selected) It creates a toggle button with the specified image and
selection state, but no text.

Example:
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.JToggleButton;

public class JToggleButtonExample extends JFrame implements ItemListener


{
public static void main(String[] args) {
new JToggleButtonExample(); }
private JToggleButton button;
JToggleButtonExample() {
setTitle("JToggleButton with ItemListener Example");
setLayout(new FlowLayout());
setJToggleButton();
setAction();
setSize(200, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setJToggleButton() {
button = new JToggleButton("ON");
add(button);
}
private void setAction() {
button.addItemListener(this);
}
public void itemStateChanged(ItemEvent eve) {
if (button.isSelected())
button.setText("OFF");
else
button.setText("ON");
}
}
8. Write Short note on java swing 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 ".
• It inherits JToggleButton class.

The declaration for javax.swing.JCheckBox class.

public class JCheckBox extends JToggleButton implements Accessible

Commonly used Constructors:

Constructor Description

JJCheckBox() Creates an initially unselected check box button with no text,


no icon.

JChechBox(String s) Creates an initially unselected check box with text.

JCheckBox(String text, boolean selected) Creates a check box with text and specifies whether or not it is
initially selected.

JCheckBox(Action a) Creates a check box where properties are taken from the
Action supplied.

Commonly used Methods:

Methods Description

AccessibleContext getAccessibleContext() It is used to get the AccessibleContext associated with this


JCheckBox.

protected String paramString() It returns a string representation of this JCheckBox.

Example:
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);
10. checkBox2.setBounds(100,150, 50,50);
11. f.add(checkBox1);
12. f.add(checkBox2);
13. f.setSize(400,400);
14. f.setLayout(null);
15. f.setVisible(true);
16. }
17. public static void main(String args[])
18. {
19. new CheckBoxExample();
20. }}
9. Write Short note on java swing 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.

JRadioButton class declaration

Let's see the declaration for javax.swing.JRadioButton class.

public class JRadioButton extends JToggleButton implements Accessible

Commonly used Constructors:

Constructor Description

JRadioButton() Creates an unselected radio button with no text.

JRadioButton(String s) Creates an unselected radio button with specified text.

JRadioButton(String s, boolean selected) Creates a radio button with the specified text and selected
status.

Commonly used Methods:

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.

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();
g 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(); }}
10. Write Short note on Java Swing JLabel:
The object of JLabel class is a component for placing text in a container.
• 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.
• It inherits JComponent class.
Example:
OUTPUT:
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);
}
}
11. Write Short note on Java Swing JTextField:

• The object of a JTextField class is a text component that allows the editing of a single line text.
• It inherits JTextComponent class.
Below is the declaration for javax.swing.JTextField class.

public class JTextField extends JTextComponent implements SwingConstants

Class JTextField : Constructors

Class JTextField : Methods

Example:
import javax.swing.*; Output:
// Main class TextFieldExample
class TextFieldExample
{
public static void main(String args[])
{
JFrame f= new JFrame("TextField Example");
JTextField t1, t2;
t1 = new JTextField("Welcome to Javatpoint.");
10. t1.setBounds(50,100, 200,30);
11. t2 = new JTextField("AWT Tutorial");
12. t2.setBounds(50,150, 200,30);
13. f.add(t1);
14. f.add(t2);
15. f.setSize(400,400);
16. f.setLayout(null);
17. f.setVisible(true);
18. }
19. }
12. Write Short note on java swing JTextArea
• The object of a JTextArea class is a multi-line region that displays text.
• It allows the editing of multiple-line text. It inherits the JTextComponent class.
• 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.

JTextArea Class Declaration


declaration for javax.swing.JTextArea class.

public class JTextArea extends JTextComponent

Commonly Used Constructors

Constructor Description

JTextArea() Creates a text area that displays no text initially.

JTextArea(String s) Creates a text area that displays specified text initially.

JTextArea(int row, int column) Creates a text area with the specified number of rows
and columns that displays no text initially.

JTextArea(String s, int row, int column) Creates a text area with the specified number of rows
and columns that displays specified text.

Example:
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();
}}
13. Write Short note on java swing JList
The object of JList class represents a list of text items. The list of text items can be set up so that the user can
choose either one item or multiple items. It inherits JComponent class.

JList class declaration


The declaration for javax.swing.JList class.
public class JList extends JComponent implements Scrollable, Accessible
Commonly used Constructors:

Constructor Description

JList() Creates a JList with an empty, read-only, model.

JList(ary[] listData) Creates a JList that displays the elements in the


specified array.

JList(ListModel<ary> dataModel) Creates a JList that displays elements from the


specified, non-null, model.

Commonly used Methods:

Methods Description

Void addListSelectionListener(ListSelectionListener It is used to add a listener to the list, to be notified


listener) each time a change to the selection occurs.

int getSelectedIndex() It is used to return the smallest selected cell index.

ListModel getModel() It is used to return the data model that holds a list of
items displayed by the JList component.

void setListData(Object[] listData) It is used to create a read-only ListModel from an


array of objects.

import javax.swing.*;
public class ListExample
{
ListExample(){
JFrame f= new JFrame();
DefaultListModel<String> l1 = new DefaultListModel<>();
l1.addElement("Item1");
l1.addElement("Item2");
l1.addElement("Item3");
l1.addElement("Item4");
JList<String> list = new JList<>(l1);
list.setBounds(100,100, 75,75);
f.add(list);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new ListExample();
}}
14. Write Short note on java swing 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. It inherits JComponent class.

JComboBox class declaration


The declaration for javax.swing.JComboBox class.
public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListener,
Accessible
Commonly used Constructors:

Constructor Description

JComboBox() Creates a JComboBox with a default data model.

JComboBox(Object[] items) Creates a JComboBox that contains the elements in the specified array.

JComboBox(Vector<?> items) Creates a JComboBox that contains the elements in the specified Vector.

Commonly used Methods:


Methods Description

void addItem(Object anObject) It is used to add an item to the item list.

void removeItem(Object anObject) It is used to delete an item to the item list.

void removeAllItems() It is used to remove all the items from the list.

void setEditable(boolean b) It is used to determine whether the JComboBox is editable.

void addActionListener(ActionListener a) It is used to add the ActionListener.

void addItemListener(ItemListener i) It is used to add the ItemListener.

Example:
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();
} }
15. Write Short note on java swing JScrollPane
• A JScrollPane is a Swing component in Java that provides a scrollable view of another
component, typically a JTextArea, JTable, JList, or any other component that implements
the Scrollable interface.
• When the contents of the component exceed the visible area, the scroll bars of the
JScrollPane allow users to scroll to view the entire contents.
Constructors

Constructor Purpose

JScrollPane() Creates an empty (no viewport view) JScrollPane where both horizontal and
vertical scrollbars appear when needed.

JScrollPane(Component) Creates a JScrollPane that displays the contents of the specified component,
where both horizontal and vertical scrollbars appear whenever the
component's contents are larger than the view.

JScrollPane(int, int) Creates an empty (no viewport view) JScrollPane with specified scrollbar
policies.

JScrollPane(Component, int, int) Creates a JScrollPane that displays the view component in a viewport whose
view position can be controlled with a pair of

Useful Methods

Modifier Method Description

void setColumnHeaderView(Component) Creates a column-header viewport if


necessary, sets its view, and then adds
the column-header viewport to the
scrollpane.

void setRowHeaderView(Component) Creates a row-header viewport if


necessary, sets its view and then adds
the row-header viewport to the
scrollpane.

void setCorner(String, Component) Adds a child that will appear in one of


the scroll panes corners, if there's
room.

Component getCorner(String) Returns the component at the specified


corner.

void setViewportView(Component) Creates a viewport if necessary and


then sets its view.
Example:
import java.awt.FlowLayout;
import javax.swing.JFrame; OUTPUT:
import javax.swing.JScrollPane;
import javax.swing.JtextArea;

public class JScrollPaneExample {


private static final long serialVersionUID = 1L;

private static void createAndShowGUI() {

// Create and set up the window.


final JFrame frame = new JFrame("Scroll Pane Example");

// Display the window.


frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// set flow layout for the frame


frame.getContentPane().setLayout(new FlowLayout());

JTextArea textArea = new JTextArea(20, 20);


JScrollPane scrollableTextArea = new JScrollPane(textArea);

scrollableTextArea.setHorizontalScrollBarPolicy(JScrollPane.HORI
ZONTAL_SCROLLBAR_ALWAYS);
scrollableTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTIC
AL_SCROLLBAR_ALWAYS);

frame.getContentPane().add(scrollableTextArea);
}
public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {


createAndShowGUI();
}
});
}
}

You might also like