0% found this document useful (0 votes)
33 views

Java 6

This document contains Java code for creating a GUI application with Swing components like buttons, labels, text fields and a table. It defines layouts and event handlers for the components. Specifically, it allows the user to view student data in a table, add/edit/delete student records by clicking buttons, and populate/update the table from a database.

Uploaded by

kpegohe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Java 6

This document contains Java code for creating a GUI application with Swing components like buttons, labels, text fields and a table. It defines layouts and event handlers for the components. Specifically, it allows the user to view student data in a table, add/edit/delete student records by clicking buttons, and populate/update the table from a database.

Uploaded by

kpegohe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

jScrollPane1.

setViewportView(jTable1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(34, 34, 34)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3))))
.addGroup(layout.createSequentialGroup()
.addGap(60, 60, 60)
.addComponent(jButton5)
.addGap(44, 44, 44)
.addComponent(jButton4)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 605,
Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 34,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 29,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 29,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0,
Short.MAX_VALUE))
.addContainerGap(14, Short.MAX_VALUE))
);

pack();
setLocationRelativeTo(null);
}// </editor-fold>

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

d = (DefaultTableModel)jTable1.getModel();
int selectIndex = jTable1.getSelectedRow();

String id = d.getValueAt(selectIndex, 0).toString();

String stname = txtstname.getText();


String pname = txtpname.getText();

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

String date = df.format(txtdob.getDate());

String gender = txtgender.getSelectedItem().toString();

String phone = txtphone.getText();

String address = txtaddress.getText();

String classes = txtclass.getSelectedItem().toString();

String section = txtsection.getSelectedItem().toString();

try {
pst = con.prepareStatement("update student set stname = ?, pname = ?, dob = ?, gender = ?,
phone = ?, address = ?, class = ?, section = ? where id = ?");

pst.setString(1,stname);
pst.setString(2,pname);
pst.setString(3,date);
pst.setString(4,gender);
pst.setString(5,phone);
pst.setString(6,address);
pst.setString(7,classes);
pst.setString(8,section);
pst.setString(9,id);

pst.executeUpdate();
JOptionPane.showMessageDialog(this, "Student Edited");
Student_load();

} catch (SQLException ex) {


Logger.getLogger(student.class.getName()).log(Level.SEVERE, null, ex);
}

// TODO add your handling code here:


}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
this.setVisible(false);
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:

String stname = txtstname.getText();


String pname = txtpname.getText();

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

String date = df.format(txtdob.getDate());

String gender = txtgender.getSelectedItem().toString();

String phone = txtphone.getText();

String address = txtaddress.getText();

String classes = txtclass.getSelectedItem().toString();

String section = txtsection.getSelectedItem().toString();

try {
pst = con.prepareStatement("insert into
student(stname,pname,dob,gender,phone,address,class,section)Values(?,?,?,?,?,?,?,?)");

pst.setString(1,stname);
pst.setString(2,pname);
pst.setString(3,date);
pst.setString(4,gender);
pst.setString(5,phone);
pst.setString(6,address);
pst.setString(7,classes);
pst.setString(8,section);

pst.executeUpdate();
JOptionPane.showMessageDialog(this, "Student Added");
Student_load();

} catch (SQLException ex) {


Logger.getLogger(student.class.getName()).log(Level.SEVERE, null, ex);
}

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {


try {
// TODO add your handling code here:

d = (DefaultTableModel)jTable1.getModel();
int selectIndex = jTable1.getSelectedRow();

String id = d.getValueAt(selectIndex, 0).toString();

txtstname.setText(d.getValueAt(selectIndex, 1).toString());
txtpname.setText(d.getValueAt(selectIndex, 2).toString());
//txtdate.(d.getValueAt(selectIndex, 3).toString());
//Date date = new SimpleDateFormat("yyyy-MM-dd").parse((String)d.getValueAt(selectIndex, 3));
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(d.getValueAt(selectIndex,
3).toString());
txtdob.setDate(date);

txtgender.setSelectedItem(d.getValueAt(selectIndex, 4).toString());
txtphone.setText(d.getValueAt(selectIndex, 5).toString());
txtaddress.setText(d.getValueAt(selectIndex, 6).toString());
txtclass.setSelectedItem(d.getValueAt(selectIndex, 7).toString());
txtsection.setSelectedItem(d.getValueAt(selectIndex, 8).toString());
jButton1.setEnabled(false);
} catch (ParseException ex) {
Logger.getLogger(student.class.getName()).log(Level.SEVERE, null, ex);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(student.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(student.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(student.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(student.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new student().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField txtaddress;
private javax.swing.JComboBox<String> txtclass;
private com.toedter.calendar.JDateChooser txtdob;
private javax.swing.JComboBox<String> txtgender;
private javax.swing.JTextField txtphone;
private javax.swing.JTextField txtpname;
private javax.swing.JComboBox<String> txtsection;
private javax.swing.JTextField txtstname;
// End of variables declaration
}

You might also like