GUI Programming
Comprehensive exam revision notes for BCA-432E Unit 3 (Object-Oriented Programming & Class Libraries in .NET). Includes Section A (2M), Section B (5M), and Section C (15M) questions, answers, and diagrams.
GUI Programming
Object-Oriented Programming & Class Libraries in .NET
Core Definitions & Key Concepts
What is the role of JTree and what model governs it
JTree displays hierarchical data in an expandable tree view with parent and leaf nodes. It is governed by the TreeModel interface (typically implemented using DefaultTreeModel and DefaultMutableTreeNode).
JTable and its data architecture
JTable renders multi-column tabular data in rows and columns. It follows MVC: data is maintained by TableModel (typically DefaultTableModel), columns by TableColumnModel, and selections by ListSelectionModel.
What is JTabbedPane
A container component that allows users to switch between multiple panels/views by clicking on tab headers, maximizing visual screen space.
JSplitPane and its orientation modes
A container that divides two components with a movable divider separator bar. Can be oriented horizontally (JSplitPane.HORIZONTAL_SPLIT - left/right) or vertically (JSplitPane.VERTICAL_SPLIT - top/bottom).
What are the core classes comprising a standard Menu System in Java Swing
JMenuBar: The horizontal bar attached to the top of a frame.JMenu: Drop-down menu item placed in the menu bar (e.g., 'File', 'Edit').JMenuItem: Individual selectable command inside a menu.JCheckBoxMenuItemandJRadioButtonMenuItem: Toggleable menu items.
What is a JPopupMenu and how is it triggered
A floating context menu that appears at the mouse cursor position, typically triggered by right-clicking on a component (e.isPopupTrigger() in mousePressed or mouseReleased).
What is JToolBar
A draggable, dockable container of buttons and icons providing quick one-click shortcuts to frequently used application commands.
What is JFileChooser
A standardized system dialog enabling users to navigate directory structures and select files for opening (showOpenDialog()) or saving (showSaveDialog()).
What is JColorChooser
A pre-built modal dialog that allows users to pick a color from palette swatches, HSL sliders, or RGB values via JColorChooser.showDialog().
What are Keyboard Accelerators vs Mnemonics in Menus
- Mnemonic: An underlined letter in a menu title activated by pressing
Alt + Keywhen menu is visible (e.g.,Alt + Ffor File). - Accelerator: A global keyboard shortcut that triggers the menu command directly even when the menu is closed (e.g.,
Ctrl + Sfor Save viaKeyStroke).
Conceptual & Architectural Analysis
The Architecture of JTable and DefaultTableModel
[ VIEW: JTable Component ]
|
+---> Displays: Headers & Rows in ScrollPane
|
+---> Queries Data From: TableModel (DefaultTableModel)
|-- getValueAt(row, col)
|-- setValueAt(val, row, col)
|-- getRowCount(), getColumnCount()
|-- isCellEditable(row, col)- Modifying data in
DefaultTableModelautomatically firesTableModelEvent, triggeringJTableto repaint only the modified cells.
Compare JTree Node Hierarchy: DefaultMutableTreeNode vs TreeModel
A JTree is structured using tree nodes:
DefaultMutableTreeNode root = new DefaultMutableTreeNode("University");- Child nodes added via
root.add(childNode);. - Path selection events are captured via
TreeSelectionListener(getSelectionPath()).
Menu Bar Hierarchy with Accelerators and Mnemonics
1JMenuBar menuBar = new JMenuBar();2JMenu fileMenu = new JMenu("File");3fileMenu.setMnemonic(KeyEvent.VK_F); // Alt + F4
5JMenuItem openItem = new JMenuItem("Open...");6openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK)); // Ctrl + O7fileMenu.add(openItem);8
9menuBar.add(fileMenu);10frame.setJMenuBar(menuBar);Standard Dialog Types in JOptionPane
- Message Dialog:
JOptionPane.showMessageDialog(parent, "Operation successful!", "Info", JOptionPane.INFORMATION_MESSAGE); - Confirm Dialog:
int choice = JOptionPane.showConfirmDialog(parent, "Delete file?", "Confirm", JOptionPane.YES_NO_OPTION); - Input Dialog:
String val = JOptionPane.showInputDialog(parent, "Enter your name:"); - Option Dialog: Custom array of buttons/options.
JSplitPane Divider Management and Nesting
1JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftTreePanel, rightTablePanel);2splitPane.setDividerLocation(200); // 200px width for left pane3splitPane.setOneTouchExpandable(true); // Adds collapse/expand arrowsIn-Depth Analysis & Implementation
Develop a Complete Enterprise MDI-Style Application with MenuBar, JToolBar, JTree, JTable, and FileChooser
package com.mjpru.bca.gui;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
public class EnterpriseWorkbench extends JFrame {
private JTable dataTable;
private DefaultTableModel tableModel;
private JTree navTree;
public EnterpriseWorkbench() {
setTitle("MJPRU Enterprise Student Workbench");
setSize(900, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
// 1. Menu Bar Construction
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
JMenuItem openItem = new JMenuItem("Open Database...");
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
openItem.addActionListener(e -> handleOpenFile());
fileMenu.add(openItem);
fileMenu.addSeparator();
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(e -> System.exit(0));
fileMenu.add(exitItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
// 2. Toolbar Construction
JToolBar toolBar = new JToolBar();
JButton addRowBtn = new JButton("Add Student");
JButton delRowBtn = new JButton("Delete Selected");
toolBar.add(addRowBtn);
toolBar.add(delRowBtn);
add(toolBar, BorderLayout.NORTH);
// 3. Navigation Tree (Left Pane)
DefaultMutableTreeNode root = new DefaultMutableTreeNode("MJPRU Campus");
DefaultMutableTreeNode bcaNode = new DefaultMutableTreeNode("BCA Department");
bcaNode.add(new DefaultMutableTreeNode("Semester 1"));
bcaNode.add(new DefaultMutableTreeNode("Semester 3"));
bcaNode.add(new DefaultMutableTreeNode("Semester 5"));
root.add(bcaNode);
navTree = new JTree(root);
JScrollPane treeScroll = new JScrollPane(navTree);
// 4. Data Table (Right Pane)
String[] columns = {"Roll No", "Name", "Subject", "Marks", "Grade"};
tableModel = new DefaultTableModel(columns, 0);
tableModel.addRow(new Object[]{"101", "Rohit Sharma", "Web Tech", 85, "A+"});
tableModel.addRow(new Object[]{"102", "Ananya Verma", "Cyber Sec", 92, "O"});
tableModel.addRow(new Object[]{"103", "Saurabh Dixit", "Comp Graphics", 78, "A"});
dataTable = new JTable(tableModel);
JScrollPane tableScroll = new JScrollPane(dataTable);
// 5. Split Pane Divider
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeScroll, tableScroll);
splitPane.setDividerLocation(220);
splitPane.setOneTouchExpandable(true);
add(splitPane, BorderLayout.CENTER);
// Actions
addRowBtn.addActionListener(e -> {
tableModel.addRow(new Object[]{"104", "New Student", "GUI Prog", 80, "A"});
});
delRowBtn.addActionListener(e -> {
int row = dataTable.getSelectedRow();
if (row != -1) tableModel.removeRow(row);
else JOptionPane.showMessageDialog(this, "Please select a row to delete.");
});
}
private void handleOpenFile() {
JFileChooser chooser = new JFileChooser();
int res = chooser.showOpenDialog(this);
if (res == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
JOptionPane.showMessageDialog(this, "Selected: " + f.getAbsolutePath());
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new EnterpriseWorkbench().setVisible(true));
}
}JPopupMenu Implementation for Contextual Actions on GUI Components
1// Context Popup Menu on JTable2JPopupMenu popupMenu = new JPopupMenu();3JMenuItem detailsItem = new JMenuItem("View Full Profile");4JMenuItem exportItem = new JMenuItem("Export to PDF");5popupMenu.add(detailsItem);6popupMenu.add(exportItem);7
8table.addMouseListener(new MouseAdapter() {9 private void checkPopup(MouseEvent e) {10 if (e.isPopupTrigger()) {11 int row = table.rowAtPoint(e.getPoint());12 if (row >= 0 && !table.isRowSelected(row)) {13 table.setRowSelectionInterval(row, row);14 }15 popupMenu.show(e.getComponent(), e.getX(), e.getY());16 }17 }18 @Override public void mousePressed(MouseEvent e) { checkPopup(e); }19 @Override public void mouseReleased(MouseEvent e) { checkPopup(e); }20});Detail Modal vs Modeless Dialog Windows with a Custom JDialog Implementation
public class PasswordResetDialog extends JDialog {
public PasswordResetDialog(Frame parent) {
// true enables MODAL mode (blocks parent frame until dismissed)
super(parent, "Reset Security Credentials", true);
setSize(300, 180);
setLocationRelativeTo(parent);
setLayout(new GridLayout(3, 1, 10, 10));
add(new JLabel("Enter New Password:"));
JPasswordField pass = new JPasswordField();
add(pass);
JButton saveBtn = new JButton("Update");
saveBtn.addActionListener(e -> dispose()); // Closes dialog
add(saveBtn);
}
}Important Exam Points & Quick Revision
Examiners frequently ask: Menu system architecture (JMenuBar, JMenu, JMenuItem, accelerators), JTable with DefaultTableModel, and Modal vs Modeless dialogs.
100% left on this page — finishing it closes the gap for Object-Oriented Programming & Class Libraries in .NET.
Object-Oriented Programming & Class Libraries in .NET still counts as a weak topic until you finish this note and answer its questions.
Practise this unitTest Your Understanding
Now that you've read through these notes, practice unit MCQs to identify weak points and solidify your learning before exams.