In "Test" folder:
SetupPenilData.java
java Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tests;
import DAO.PencilsApplicationDAO;
import DAO.PencilDAO;
import java.sql.*;
/**
*
*/
public class SetupPencilData {
try {
PencilsApplicationDAO.connectToDatabase();
System.
out.
println("UNABLE to connect to database.");
}
s = con.createStatement();
// Create tables
try {
sqlSt = "DROP TABLE tblPencils";
s.executeUpdate(sqlSt);
if (ex.getMessage().contains("Unknown table") || ex.getMessage().contains("does not exist")) {
System.
out.
println("code="+ex.
getErrorCode());
System.
out.
println("TABLE tblPencils does not exist but will be created.");
} else {
ex.printStackTrace();
}
}
System.
out.
println("CREATING TABLES...");
sqlSt = "CREATE TABLE tblPencils (colour VARCHAR(50), size INT)";
s.executeUpdate(sqlSt);
int colourKey;
System.
out.
println("INSERTING pencil records...");
//INSERT Pencils Green
sqlSt = "INSERT into tblPencils values ('GREEN',10), ('GREEN',10), ('BLUE',20), ('RED',15), ('GREEN',10), ('BLACK',8)";
s.executeUpdate(sqlSt);
con.close();
}
}
Files in the View:
PencilJTableViewPanel.java
java Code:
package view;
import model.PencilJTableModel;
import java.awt.BorderLayout;
import javax.swing.*;
import model.Pencil;
import java.util.List;
/**
* The JPanel for the JTable
*/
public class PencilJTableViewPanel
extends JPanel {
/**
* Cretae the Panel for the JTable. Placing the JTable in a JScrollPane
*/
public PencilJTableViewPanel() {
dataTable.setModel(new PencilJTableModel());
}
/**
*
* @return the model currently being used by the JTable
*/
public PencilJTableModel getModel() {
return (PencilJTableModel) (dataTable.getModel());
}
/**
* Change the data used by the JTable
* @param model The new data for the JTable
*/
public void setModel(PencilJTableModel model) {
dataTable.setModel(model);
}
/**
* Change the data used by the JTable
* @param model The new data for the JTable
*/
public void setData(List<Pencil> data) {
this.getModel().setData(data);
}
}
PencilMainViewFrame.java:
java Code:
package view;
import controller.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import model.*;
/**
* Displays a simple GUI allowing user to view a table of pencils of a
* chosen colour
*/
public class PencilMainViewFrame
extends JFrame {
private PencilJTableViewPanel panData;
private PencilApplication theController;
/**
* Create the view Frame for this application
* @param theController Logic will be handled by this controller
* @param lstModel The data for for the JList
* @param tabModel The data for the JTable
*/
public PencilMainViewFrame(PencilApplication theController) {
super("COLOUR PENCILS");
this.theController = theController;
this.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
lstColours.setModel(new ColourComboBoxModel(null));
lblColours =
new JLabel("Choose a colour: ");
//Setup Button panel
panButtons.add(lblColours);
panButtons.add(lstColours);
// Setup books data panel
panData = new PencilJTableViewPanel();
//Add panels to frame
// Register event handlers
lstColours.addActionListener(new ComboPublisherEventHandler());
//Finish off
setSize(600, 250);
setVisible(true);
}
/**
* When the controller chooses to this will display new Book data in the JTable
* @param bookJTableModel The new data for the JTable
*/
public void displayNewPencilData(java.util.List<Pencil> data) {
panData.setData(data);
}
/**
* When the controller chooses to this will display an error connecting to database message
* @param bookJTableModel The new data for the JTable
*/
public void displayDatabaseConnectionError() {
JOptionPane.
showMessageDialog(this,
"UNABLE TO CONNECT TO DATABASE");
}
/**
* When the controller chooses to this will display an error connecting to database message
* @param bookJTableModel The new data for the JTable
*/
public void displayDatabaseUnExpectedError
(String message
) { JOptionPane.
showMessageDialog(this,
"UNEXPECTED DATABASE ERROR HAS OCCURED\n" + message
);
}
/**
* Change the data used by the JComboBox
* @param model The new data for the JComboBox
*/
public void displaynewListOfColours(java.util.List<String> colours) {
((ColourComboBoxModel)lstColours.getModel()).setData(colours);
}
/**
* Inner class eventHandler for the JComboBox of Colours
*/
/**
* Responds to a new publisher being chosen from the JList
* @param e Details of the event
*/
chosenColour =
(String) (lstColours.
getModel().
getSelectedItem());
theController.displaycolourpencils(chosenColour);
}
}
}