List界面的实现
List将JTable引入JScrollPane,实现上下滑动
实现列表的显示
public void setTable(StudentMessage studentmessage) {
DefaultTableModel dfb = (DefaultTableModel) table.getModel();
dfb.setRowCount(0);
StuSql stusql = new StuSql();
List<StudentMessage> StudentList = stusql.getStudentList(studentmessage);
for(StudentMessage sm : StudentList) {
Vector v = new Vector();
v.add(sm.getId());
v.add(sm.getStudentName());
v.add(sm.getClassName());
v.add(sm.getSex());
v.add(sm.getInfo());
v.add(sm.getTime());
dfb.addRow(v);
}
对应的数据库操作
public List<StudentMessage> getStudentList(StudentMessage studentmessage){
List<StudentMessage> reList = new ArrayList<StudentMessage>();
StringBuffer SqlString = new StringBuffer("select * from StudentMessage ");
if(!StringUtil.isEmpty(studentmessage.getStudentName())) {
SqlString.append(" where name like '%"+studentmessage.getStudentName()+"%'");
SqlString.append("and className like '%" + studentmessage.getClassName()+"%'");
}
try {
PreparedStatement prst = con.prepareStatement(SqlString.toString());
ResultSet rs = prst.executeQuery();
while(rs.next()) {
StudentMessage sm = new StudentMessage();
sm.setId(rs.getInt(1));
sm.setStudentName(rs.getString(2));
sm.setClassName(rs.getString(3));
sm.setSex(rs.getString(4));
sm.setInfo(rs.getString(5));
sm.setTime(rs.getString(6));
reList.add(sm);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return reList;
}
edit界面实现
table = new JTable();
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
int selectitem = table.getSelectedRow();
Object oa = model.getValueAt(selectitem, 0);
Object ab = model.getValueAt(selectitem, 1);
Object oc = model.getValueAt(selectitem, 2);
Object od = model.getValueAt(selectitem, 3);
Object oe = model.getValueAt(selectitem, 4);
Object of = model.getValueAt(selectitem, 5);
Object[] obj = new Object[] {oa,ab,oc,od,oe,of};
//对选中的行操作,判断鼠标点击次数
if(e.getClickCount()==2) {
//进入edit界面
new EditStudentMessageFrame(obj).setVisible(true);
}}});
对是否选中进行判断
protected void edit(ActionEvent e) {
// TODO Auto-generated method stub
try { DefaultTableModel model = (DefaultTableModel) table.getModel();
int selectitem = table.getSelectedRow();
Object oa = model.getValueAt(selectitem, 0);
Object ab = model.getValueAt(selectitem, 1);
Object oc = model.getValueAt(selectitem, 2);
Object od = model.getValueAt(selectitem, 3);
Object oe = model.getValueAt(selectitem, 4);
Object of = model.getValueAt(selectitem, 5);
Object[] obj = new Object[] {oa,ab,oc,od,oe,of};
if(table.isRowSelected(selectitem)) {
new EditStudentMessageFrame(obj).setVisible(true);
}
}catch(Exception ae) {
ae.printStackTrace();
//捕获异常,返回提示信息
MsgBox.showInfo("您未选中任何一项,请选中后再进行操作");
}
}