I need help with pagination in Java is there any possible way to implement pagination with these sets of codes? My current table is filled with data that is linked to my sql.
public TestTableModel(ArrayList<Patient> arrListOfPatient) {
rowCount = arrListOfPatient.size();
colCount = columnNames.length;
data = new Object[rowCount][colCount];
for (int i = 0; i < rowCount; i++) {
// Copy an ArrayList element to an instance of Expenses
Patient p = (Patient) (arrListOfPatient.get(i));
data[i][0] = p.getId();
data[i][1] = p.getName();
}
}
public TestTableModel() {
rowCount = data.length;
colCount = columnNames.length;
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return colCount;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return rowCount;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return data[rowIndex][columnIndex];
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
@Override
public String getColumnName(int col) {
return columnNames[col].toString();
}
and the jpanel below calls the table above and puts the abstract table into a jtable right now it is able to display image when inputted, create udpdate and delete entries
public class PatientTable extends master {
/**
*
*/
private static final long serialVersionUID = 1L;
private JComboBox name;
private ArrayList<Patient> patientList;
private PatientTableModel tableModel;
private JTable tblPatient;
private JLabel imgDisplay;
private JTextField ageField;
private JTextField heightField;
private JTextField weightField;
private JTextField genderField;
private JTextField raceField;
private JTextField nameField;
public PatientTable(JFrame mf) {
super(mf);
patientList = PatientDA.retrieveAllPatient();
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportBorder(null);
scrollPane.setBounds(12, 80, 497, 174);
add(scrollPane);
tblPatient = new JTable();
PatientTableModel ttm = new PatientTableModel();
scrollPane.setViewportView(tblPatient);
JButton btnRetrieve = new JButton("Retrieve");
btnRetrieve.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// tableModel = new ExpenseTableModel();
// tblExpense.setModel(tableModel);
retrieveAllPatient();
}
});
btnRetrieve.setBounds(12, 267, 89, 23);
add(btnRetrieve);
JButton btnDelete = new JButton("Delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionPerformedDelete();
}
});
JButton btnSelect = new JButton("Select");
btnSelect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int rowSelected = tblPatient.getSelectedRow();
fillPatientForm(rowSelected);
getImage();
}
});
btnSelect.setBounds(225, 266, 97, 25);
add(btnSelect);
btnDelete.setBounds(113, 267, 89, 23);
add(btnDelete);
imgDisplay = new JLabel("");
imgDisplay.setBounds(633, 94, 250, 210);
add(imgDisplay);
JButton btnDisplay = new JButton("Display");
btnDisplay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
getImage();
}
});
btnDisplay.setBounds(532, 94, 89, 23);
add(btnDisplay);
JLabel label = new JLabel("age");
label.setFont(new Font("Bookman Old Style", Font.PLAIN, 30));
label.setBounds(41, 378, 48, 48);
add(label);
ageField = new JTextField();
ageField.setColumns(10);
ageField.setBounds(155, 398, 130, 20);
add(ageField);
JLabel label_1 = new JLabel("height");
label_1.setFont(new Font("Bookman Old Style", Font.PLAIN, 30));
label_1.setBounds(41, 425, 91, 39);
add(label_1);
heightField = new JTextField();
heightField.setColumns(10);
heightField.setBounds(159, 440, 126, 20);
add(heightField);
JLabel label_2 = new JLabel("weight");
label_2.setFont(new Font("Bookman Old Style", Font.PLAIN, 30));
label_2.setBounds(41, 477, 94, 36);
add(label_2);
weightField = new JTextField();
weightField.setColumns(10);
weightField.setBounds(159, 491, 126, 20);
add(weightField);
genderField = new JTextField();
genderField.setColumns(10);
genderField.setBounds(437, 342, 126, 20);
add(genderField);
raceField = new JTextField();
raceField.setColumns(10);
raceField.setBounds(437, 398, 126, 20);
add(raceField);
JLabel label_3 = new JLabel("name");
label_3.setFont(new Font("Bookman Old Style", Font.PLAIN, 30));
label_3.setBounds(41, 323, 80, 47);
add(label_3);
JLabel label_4 = new JLabel("gender");
label_4.setFont(new Font("Bookman Old Style", Font.PLAIN, 30));
label_4.setBounds(310, 325, 104, 43);
add(label_4);
JLabel label_5 = new JLabel("race");
label_5.setFont(new Font("Bookman Old Style", Font.PLAIN, 30));
label_5.setBounds(320, 378, 66, 48);
add(label_5);
JButton updateBtn = new JButton("Update");
updateBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Patient p = new Patient();
updatePatient(p.getId());
}
});
updateBtn.setBounds(483, 489, 91, 25);
add(updateBtn);
/* name = new JComboBox();
name.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cbo = (JComboBox) e.getSource();
int selectedIndex = cbo.getSelectedIndex();
fillPatientForm(selectedIndex);
}
});
name.setBounds(155, 342, 130, 20);
add(name);
this.fillName(); */
nameField = new JTextField();
nameField.setColumns(10);
nameField.setBounds(159, 341, 126, 20);
add(nameField);
JLabel bckground = new JLabel("");
bckground.setIcon(new ImageIcon(homePage.class.getResource("/appcrux/images/Bckgrnd.png")));
bckground.setBounds(0, 13, 1000, 600);
add(bckground);
}
private void retrieveAllPatient() {
ArrayList<Patient> Patient = PatientDA.retrieveAllPatient();
tableModel = new PatientTableModel(Patient);
tblPatient.setModel(tableModel);
}
public void actionPerformedDelete(){
int rowSelected = tblPatient.getSelectedRow();
if(rowSelected >= 0){
int resp = JOptionPane.showConfirmDialog(masterFrame, "Confirm Delete?", "Confirmation", JOptionPane.YES_NO_OPTION);
if(resp == JOptionPane.OK_OPTION){
int id = (Integer)tblPatient.getModel().getValueAt(rowSelected, 0);
PatientDA.deletePatient(id);
retrieveAllPatient();
}
}
}
public void getImage(){
int rowSelected = tblPatient.getSelectedRow();
int id = (Integer)tblPatient.getModel().getValueAt(rowSelected, 0);
String url = "";
try{
url = "jdbc:mysql://localhost/appcrux";
java.sql.Connection con = DriverManager.getConnection(url, "root", "oopp");
java.sql.Statement st = con.createStatement();
System.out.println("Successfully connected to " + url+ ".");
ResultSet rs = st.executeQuery("SELECT * FROM patient where id = '"+id+"'");
if(rs.next()){
byte[] img = rs.getBytes("image");
String images = new String (img);
//Resize The ImageIcon
ImageIcon image1 = new ImageIcon(images);
Image im = image1.getImage();
Image myImg = im.getScaledInstance(200,200,Image.SCALE_SMOOTH);
ImageIcon newImage = new ImageIcon(myImg);
imgDisplay.setIcon(newImage);
}
else{
JOptionPane.showMessageDialog(null, "No Data");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
private void fillPatientForm(int selectedIndex) {
Patient p = patientList.get(selectedIndex);
this.nameField.setText(p.getName());
this.ageField.setText(Integer.toString(p.getAge()));
this.weightField.setText(Double.toString(p.getWeight()));
this.heightField.setText(Double.toString(p.getHeight()));
this.raceField.setText(p.getRace());
this.genderField.setText(p.getGender());
}
private void fillName() {
String name = null;
Patient patient = null;
for (int i = 0; i < this.patientList.size(); i++) {
patient = patientList.get(i);
name = patient.getName();
this.name.addItem(name);
}
}
private void updatePatient(int id) {
String name = (String) this.name.getSelectedItem();
int age = Integer.parseInt(this.ageField.getText());
double height = Double.parseDouble(this.heightField.getText());
double weight = Double.parseDouble(this.weightField.getText());
String race = this.raceField.getText();
String gender = this.genderField.getText();
String img = "";
byte[] image = img.getBytes();
Patient p = new Patient(id + 1,name, age , height , weight , race, gender,image);
if (PatientDA.updatePatient(p)) {
JOptionPane.showMessageDialog(masterFrame, "Updated!");
JPanel contentPanel = new PatientTable(masterFrame);
masterFrame.setContentPane(contentPanel);
masterFrame.setVisible(true);
} else {
JOptionPane.showMessageDialog(masterFrame, "Failed!");
}
}