Since you mentioned "students data" but used item properties (item_id, name_item, sub_total), I designed this as a student-themed billing/fees table (e.g., tracking student tuition or itemized fees).
1. The Model (StudentItem.java)
This class represents a single data record.
Java
package com.example.mvc;
public class StudentItem {
private String itemId;
private String nameItem;
private double subTotal;
public StudentItem(String itemId, String nameItem, double subTotal) {
this.itemId = itemId;
this.nameItem = nameItem;
this.subTotal = subTotal;
}
// Getters and Setters
public String getItemId() { return itemId; }
public void setItemId(String itemId) { this.itemId = itemId; }
public String getNameItem() { return nameItem; }
public void setNameItem(String nameItem) { this.nameItem = nameItem; }
public double getSubTotal() { return subTotal; }
public void setSubTotal(double subTotal) { this.subTotal = subTotal; }
}
2. The View (StudentView.java)
This builds the GUI using standard Swing components. You can easily replicate this layout in the NetBeans GUI Builder by dragging a JTable into a JScrollPane (Center) and a JLabel into a JPanel (South).
Java
package com.example.mvc;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
public class StudentView extends JFrame {
private JTable table;
private DefaultTableModel tableModel;
private JLabel statusBar;
public StudentView() {
setTitle("Student Items Management (MVC)");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Setup Table
String[] columns = {"Item ID", "Student / Item Name", "Sub Total"};
tableModel = new DefaultTableModel(columns, 0);
table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
// Setup Status Bar
statusBar = new JLabel(" Total Sum: $0.00", SwingConstants.LEFT);
statusBar.setBorder(BorderFactory.createEtchedBorder());
statusBar.setPreferredSize(new Dimension(this.getWidth(), 25));
// Layout Layout
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(statusBar, BorderLayout.SOUTH);
}
public DefaultTableModel getTableModel() {
return tableModel;
}
public void updateTotalStatus(double total) {
statusBar.setText(String.format(" Total Sum: $%.2f", total));
}
}
3. The Controller (StudentController.java)
The controller bridges the Model and the View. It populates the 10 sample student items, updates the table UI, and calculates the running sum for the status bar.
Java
package com.example.mvc;
import java.util.ArrayList;
import java.util.List;
public class StudentController {
private List<StudentItem> modelList;
private StudentView view;
public StudentController(StudentView view) {
this.view = view;
this.modelList = new ArrayList<>();
// Load the 10 sample student records
loadSampleData();
// Update the GUI components
updateView();
}
private void loadSampleData() {
modelList.add(new StudentItem("STU001", "Alex - Tuition Fees", 1200.50));
modelList.add(new StudentItem("STU002", "Blake - Textbooks", 150.00));
modelList.add(new StudentItem("STU003", "Charlie - Lab Equipment", 85.75));
modelList.add(new StudentItem("STU004", "Diana - Parking Permit", 50.00));
modelList.add(new StudentItem("STU005", "Ethan - Student Activity", 110.00));
modelList.add(new StudentItem("STU006", "Fiona - Meal Plan", 500.25));
modelList.add(new StudentItem("STU007", "George - Laptop Rental", 250.00));
modelList.add(new StudentItem("STU008", "Hannah - Graduation Fee", 90.00));
modelList.add(new StudentItem("STU009", "Ian - Sports Club", 65.00));
modelList.add(new StudentItem("STU010", "Julia - Tech Services", 45.00));
}
public void updateView() {
// Clear existing rows
view.getTableModel().setRowCount(0);
double totalSum = 0;
// Populate table and calculate total sum
for (StudentItem item : modelList) {
Object[] rowData = { item.getItemId(), item.getNameItem(), item.getSubTotal() };
view.getTableModel().addRow(rowData);
totalSum += item.getSubTotal();
}
// Send sum total to view's status bar
view.updateTotalStatus(totalSum);
}
}
4. Application Entry Point (Main.java)
This ties everything together and runs the program.
Java
package com.example.mvc;
import javax.swing.SwingUtilities;
public class Main {
public static void main(String[] args) {
// Run Swing apps on the Event Dispatch Thread (EDT) for thread safety
SwingUtilities.invokeLater(() -> {
StudentView view = new StudentView();
// Controller handles initializing and linking the data
new StudentController(view);
view.setVisible(true);
});
}
}
Tidak ada komentar:
Posting Komentar