Java study Material Unit V April - May 2025
Java study Material Unit V April - May 2025
UNIT V
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.
JPanel The Swing class that defines a panel, which is used to hold other components.
Top-Level Containers
• 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:
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.
Constructor Description
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
Constructor Description
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.
Example:
Nested Classes
Modifier and Type Class Description
Constructor
Constructor Description
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;
Constructor Description
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.
Methods Description
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.
Constructor Description
JRadioButton(String s, boolean selected) Creates a radio button with the specified text and selected
status.
Methods Description
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.
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.
Constructor Description
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.
Constructor Description
Methods Description
ListModel getModel() It is used to return the data model that holds a list of
items displayed by the JList component.
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.
Constructor Description
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.
void removeAllItems() It is used to remove all the items from the list.
Example:
import javax.swing.*;
public class ComboBoxExample {
JFrame f;
ComboBoxExample(){
f=new JFrame("ComboBox Example");
String country[]={"India","Aus","U.S.A","England","Newzealand"};
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
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() {