BookController.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 bookdb;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BookController {
private final BookModel model;
private final BookView view;
public BookController(BookModel model, BookView view) {
this.model = model;
this.view = view;
// Tell the view to forward "Add" button clicks here
this.view.addRegisterListener(new AddBookListener());
}
// Inner class to handle button clicks
private class AddBookListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String title = view.getBookTitle();
String author = view.getBookAuthor();
// Simple validation
if (title.isEmpty() || author.isEmpty()) {
JOptionPane.showMessageDialog(view,
"Both Title and Author are required!",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
// Update Model (which automatically updates JTable via firing events)
model.addBook(title, author);
// Clear View UI inputs
view.clearInputs();
}
}
}
========================================
BookModel.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 bookdb;
import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;
// Simple Data Object
class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() { return title; }
public String getAuthor() { return author; }
}
// The Table Model that JTable communicates with
public class BookModel extends AbstractTableModel {
private final List<Book> books = new ArrayList<>();
private final String[] columnNames = {"Title", "Author"};
public BookModel() {
// Sample Data
books.add(new Book("The Hobbit", "J.R.R. Tolkien"));
books.add(new Book("184", "George Orwell"));
}
public void addBook(String title, String author) {
books.add(new Book(title, author));
// Notify the JTable that data has changed so it refreshes UI
fireTableDataChanged();
}
@Override
public int getRowCount() { return books.size(); }
@Override
public int getColumnCount() { return columnNames.length; }
@Override
public String getColumnName(int column) { return columnNames[column]; }
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Book book = books.get(rowIndex);
if (columnIndex == 0) return book.getTitle();
if (columnIndex == 1) return book.getAuthor();
return null;
}
}
========================================
BookView.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 bookdb;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class BookView extends JFrame {
private final JTable table;
private final JTextField titleField = new JTextField(15);
private final JTextField authorField = new JTextField(15);
private final JButton addButton = new JButton("Add Book");
public BookView(BookModel model) {
setTitle("Book Database (MVC)");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 400);
setLocationRelativeTo(null);
// Table setup
table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
// Input Panel setup
JPanel inputPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0; gbc.gridy = 0;
inputPanel.add(new JLabel("Title:"), gbc);
gbc.gridx = 1;
inputPanel.add(titleField, gbc);
gbc.gridx = 0; gbc.gridy = 1;
inputPanel.add(new JLabel("Author:"), gbc);
gbc.gridx = 1;
inputPanel.add(authorField, gbc);
gbc.gridx = 1; gbc.gridy = 2;
inputPanel.add(addButton, gbc);
// Layout integration
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(inputPanel, BorderLayout.SOUTH);
}
// Getters for Controller to fetch input data
public String getBookTitle() { return titleField.getText().trim(); }
public String getBookAuthor() { return authorField.getText().trim(); }
public void clearInputs() {
titleField.setText("");
authorField.setText("");
}
// Wire up the button to the Controller listener
public void addRegisterListener(ActionListener listener) {
addButton.addActionListener(listener);
}
}
========================================================
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 bookdb;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
// Run UI initialization on the Event Dispatch Thread for thread safety
SwingUtilities.invokeLater(() -> {
// 1. Instantiate Model
BookModel model = new BookModel();
// 2. Instantiate View and pass Model to the JTable
BookView view = new BookView(model);
// 3. Instantiate Controller to link them together
new BookController(model, view);
// 4. Show the UI
view.setVisible(true);
});
}
}
Tidak ada komentar:
Posting Komentar