Module 7 : GUI Programming, Lambdas
and Streams:
Outline
• Introduction to Annotation,
• Byte streams and character streams,
• Wrapper classes
• Lambda Expression
• Swing
Introduction to Annotation
Definition:
Annotations are metadata added to Java code to provide
additional information to the compiler or runtime.
Purpose: Used for code documentation, compiler instructions, or
at runtime to affect the behavior of programs.
Examples: @Override, @Deprecated, @SuppressWarnings
Example Code:
@Override
public void display()
{
[Link]('Overriding the display method');
}
Byte Streams and Character Streams
Byte Streams:
Handle 8-bit bytes, ideal for binary data like images, audio, etc.
Example Code for Byte Stream:
FileInputStream inputStream = new
FileInputStream('[Link]');
int data = [Link]();
while(data != -1)
{
[Link]((char) data);
data = [Link]();
}
[Link]();
Character Streams: Handle 16-bit characters, suitable for text
data.
Example Code for Character Stream:
FileReader fileReader = new FileReader('[Link]');
int data = [Link]();
while(data != -1)
{
[Link]((char) data);
data = [Link]();
}
[Link]();
Wrapper Classes
Wrapper classes convert primitive data types into objects (e.g.,
Integer, Double).
Autoboxing and Unboxing: Automatic conversion between
primitives and their corresponding wrapper classes.
Example Code:
Integer num = 5; // Autoboxing (primitive to object)
int value = num; // Unboxing (object to primitive)
Why Lambda Expression?
Benefits: Simplify code, reduce boilerplate, enable functional
programming.
Use Cases: Suitable for scenarios like iteration, event handling,
and stream processing.
Example Code Without Lambda:
List<Integer> numbers = [Link](1, 2, 3, 4, 5);
for(Integer n : numbers)
{
[Link](n);
}
With Lambda Expression:
[Link](n -> [Link](n));
Lambda Expression Syntax
Syntax:
(parameters) -> expression or (parameters) -> { statements; }
Represents a concise way to define anonymous functions.
Example Code:
(int x, int y) -> x + y
Where to Use Lambda Expression
• Suitable for operations like filtering, sorting, and transforming
data collections.
• Simplifies working with Java's Stream API and event-driven
programming.
Example Code Using Lambda with Streams:
List<String> names = [Link]('Alice', 'Bob', 'Charlie');
[Link]().filter(name -> [Link]('A')).forEach([Link]::println);
Adopting Patterns: Matching, Finding, and
Filtering
Matching: Operations like allMatch, anyMatch, noneMatch.
Finding: Using methods like findFirst and findAny.
Filtering: Filtering data based on conditions.
Example Code for Filtering:
List<Integer> numbers = [Link](3, 5, 7, 8, 10);
[Link]().filter(n -> n % 2 == 0).forEach([Link]::println);
Swing Overview
Swing:
A GUI toolkit for Java, offering rich user interface components.
Java 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.
Features: Lightweight, platform-independent, and offers a wide
range of controls.
Example Components: JButton, JLabel, JTextField.
Hierarchy of Java Swing classes
Swing Practical Overview
Swing Component Classes -
AbstractButton and ButtonGroup
AbstractButton: Base class for button controls.
ButtonGroup: Groups buttons to allow only one to be selected
at a time.
Example Code:
JRadioButton rb1 = new JRadioButton('Option 1');
JRadioButton rb2 = new JRadioButton('Option 2');
ButtonGroup group = new ButtonGroup();
[Link](rb1);
[Link](rb2);
Swing Component Classes -
ImageIcon and JApplet
ImageIcon: Represents images that can be used in GUI
components.
JApplet: Special Swing component for creating applets.
Example Code for ImageIcon:
ImageIcon icon = new ImageIcon('[Link]');
JLabel label = new JLabel(icon);
Swing Component Classes - JButton
and JCheckBox
JButton: Used for creating buttons to trigger actions.
JCheckBox: Allows multiple selections.
Example Code:
JCheckBox checkBox = new JCheckBox('Accept Terms');
JButton button = new JButton('Submit');
Swing Component Classes -
JComboBox and JLabel
JComboBox: Dropdown menu to select one item.
JLabel: Display text or images.
Example Code:
JComboBox<String> comboBox = new JComboBox<>(new
String[]{'Item 1', 'Item 2', 'Item 3'});
JLabel label = new JLabel('Select an Item');
Swing Component Classes -
JRadioButton and JScrollPane
JRadioButton: Mutually exclusive selection options.
JScrollPane: Scrollable view for large content.
Example Code:
JScrollPane scrollPane = new JScrollPane(new JTextArea(10, 20));
Swing Component Classes -
JTabbedPane and JTable
JTabbedPane: Allows multiple tabs in one window.
JTable: Represents data in a tabular format.
Example Code:
JTable table = new JTable(data, columnNames);
JTabbedPane tabbedPane = new JTabbedPane();
Swing Component Classes -
JTextField and JTree
JTextField: Used for single-line text input.
JTree: Displays hierarchical data.
Example Code:
JTextField textField = new JTextField(20);
JTree tree = new JTree();