Here is a clean, modular Model-View-Controller (MVC) implementation in Java Swing.
To keep this easy to run and understand, this example is structured in a single file containing all components. However, when working in NetBeans, you would typically split these into separate files (Student.java, StudentView.java, StudentController.java, and Main.java) within the same package.
The MVC Architecture
Model (
Student): Holds the data structure for a student.View (
StudentView): Handles the UI components (the Swing window, table, and buttons).Controller (
StudentController): Acts as the bridge, fetching the hardcoded array of 10 students and populating the View.
The Java Code
Java
Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mvcstudentapp;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
// ==========================================
// 1. THE MODEL
// ==========================================
class Student {
private String studentId;
private String name;
private String classroom;
public Student(String studentId, String name, String classroom) {
this.studentId = studentId;
this.name = name;
this.classroom = classroom;
}
// Getters
public String getStudentId() { return studentId; }
public String getName() { return name; }
public String getClassroom() { return classroom; }
}
// ==========================================
// 2. THE VIEW
// ==========================================
class StudentView extends JFrame {
private JTable studentTable;
private DefaultTableModel tableModel;
private JButton loadButton;
public StudentView() {
// Window Setup
setTitle("Student Directory (MVC Pattern)");
setSize(500, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout(10, 10));
// Table Setup
String[] columnNames = {"Student ID", "Name", "Classroom"};
tableModel = new DefaultTableModel(columnNames, 0);
studentTable = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(studentTable);
add(scrollPane, BorderLayout.CENTER);
// Controls Setup
JPanel panelBottom = new JPanel(new FlowLayout(FlowLayout.CENTER));
loadButton = new JButton("Load Student Data");
panelBottom.add(loadButton);
add(panelBottom, BorderLayout.SOUTH);
}
// Update the table with new data
public void setTableData(List<Student> students) {
tableModel.setRowCount(0); // Clear existing data
for (Student student : students) {
Object[] row = {
student.getStudentId(),
student.getName(),
student.getClassroom()
};
tableModel.addRow(row);
}
}
// Getter for the button to attach listener in Controller
public JButton getLoadButton() {
return loadButton;
}
}
// ==========================================
// 3. THE CONTROLLER
// ==========================================
class StudentController {
private StudentView view;
private List<Student> studentList;
public StudentController(StudentView view) {
this.view = view;
this.studentList = new ArrayList<>();
// Initialize 10 sample student data points
initializeSampleData();
// Attach action listener to the View's button
this.view.getLoadButton().addActionListener(e -> {
view.setTableData(studentList);
});
}
private void initializeSampleData() {
studentList.add(new Student("STU001", "Alex Mercer", "12-A"));
studentList.add(new Student("STU002", "Beatrice Vance", "12-B"));
studentList.add(new Student("STU003", "Charles Xavier", "11-A"));
studentList.add(new Student("STU004", "Diana Prince", "10-C"));
studentList.add(new Student("STU005", "Ethan Hunt", "12-A"));
studentList.add(new Student("STU006", "Fiona Gallagher", "11-B"));
studentList.add(new Student("STU007", "Garrus Vakarian", "12-B"));
studentList.add(new Student("STU008", "Hal Jordan", "10-A"));
studentList.add(new Student("STU009", "Iris West", "11-C"));
studentList.add(new Student("STU010", "Jack Sparrow", "10-B"));
}
}
// ==========================================
// MAIN ENTRY POINT
// ==========================================
public class Main {
public static void main(String[] args) {
// Set Look and Feel to System Default for a cleaner UI
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ignored) {}
SwingUtilities.invokeLater(() -> {
StudentView view = new StudentView();
// The Controller links the View and manages the data
new StudentController(view);
view.setVisible(true);
});
}
}
Tidak ada komentar:
Posting Komentar