Object-Oriented Programming in Java

Java Library & Swing GUI


Module VIII: The Java Library and GUI Programming with Swing. This module introduces essential parts of the Java Standard Library—string handling, math utilities, collections—and then provides a practical introduction to graphical programming using Swing components.


String Handling in Java

Java provides three main classes for working with textual data: String (immutable), StringBuffer (mutable, synchronized), and StringBuilder (mutable, unsynchronized).

  • String objects cannot be changed once created
  • StringBuffer is used when thread-safety is required
  • StringBuilder is faster but not synchronized
String s = "Hello";
String s2 = s.replace("H", "Y"); // new object

StringBuffer sb = new StringBuffer("Java");
sb.append(" Programming");

StringBuilder sb2 = new StringBuilder("Fast");
sb2.append(" Builder");

Diagram: string-vs-stringbuffer.png


Exploring java.lang

The java.lang package is automatically imported and contains core classes essential to Java programming.

  • Math – mathematical functions
  • Object – root of all classes
  • System – standard I/O and environment
  • Wrapper classes: Integer, Double, Character
double r = Math.sqrt(49);        // 7.0
int m = Math.max(5, 12);          // 12
char c = Character.toUpperCase('a');

Wrapper classes help convert between primitives and objects.


The Collections Framework

The collections framework provides data structures like lists, sets, queues, and maps. Generics are heavily used here.

  • ArrayList – dynamic array
  • LinkedList – doubly-linked list
  • HashSet – stores unique elements
  • HashMap – key-value storage
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");

HashMap<String, Integer> marks = new HashMap<>();
marks.put("Math", 95);
marks.put("Physics", 88);

Diagram: collections-hierarchy.png


Introduction to Swing

Swing is Java’s lightweight GUI toolkit built on top of AWT. Components are platform-independent and provide modern GUI widgets.

  • JFrame – main window
  • JButton – clickable button
  • JLabel – display text
  • JTextField – input field
Simple Swing Program
import javax.swing.*;

public class FirstGUI {
  public static void main(String[] args) {
    JFrame f = new JFrame("Demo");
    JButton b = new JButton("Click");
    b.setBounds(50, 80, 100, 40);

    f.add(b);
    f.setSize(300, 200);
    f.setLayout(null);
    f.setVisible(true);
  }
}

Each Swing program must run on the Event Dispatch Thread (EDT).


Exploring Swing Components

  • JPanel – container for grouping components
  • JMenu, JMenuItem – for building menus
  • JTextArea – multi-line text box
  • JScrollPane – adds scrollbars
Menu Example
JFrame f = new JFrame();
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem exit = new JMenuItem("Exit");

file.add(exit);
mb.add(file);
f.setJMenuBar(mb);

Diagram: swing-components-overview.png


Summary

This module introduces essential parts of the Java Standard Library and gives a practical foundation in GUI application development with Swing. Mastering strings, the math library, collections, and Swing provides the toolkit needed for building desktop applications and Core Java utilities.

Add diagrams: awt-vs-swing.png,swing-event-flow.png