簡單的list介面和edit介面使用java swing實現

傑克的鳥發表於2020-10-12

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("您未選中任何一項,請選中後再進行操作");
				}
		}

相關文章