void tableInitialise(JTable table) { JScrollPane scroll; TableColumn column = null; int colunms = table.getColumnCount(); for (int y = 0; y < colunms; y++) { column = table.getColumnModel().getColumn(y); /*将每一列的默认宽度设置为100*/ column.setPreferredWidth(100); } /* * 设置JTable自动调整列表的状态,此处设置为关闭 */ table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); table.setFont(new Font("Menu.font", Font.PLAIN, 14)); table.getTableHeader().setFont(new Font("Menu.font", Font.BOLD, 15)); /*用JScrollPane装载JTable,这样超出范围的列就可以通过滚动条来查看*/ scroll = new JScrollPane(table); TablePanel.removeAll(); TablePanel.setLayout(new BoxLayout(TablePanel, BoxLayout.Y_AXIS)); TablePanel.add(scroll); TablePanel.revalidate(); table.setRowSelectionAllowed(true); // 设置JTable可被选择 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 设置JTable为单行选择 table.getTableHeader().setBackground(new Color(206, 231, 255)); // 设置JTable表头颜色 table.getTableHeader().setReorderingAllowed(false); // 设置JTable每个字段的顺序不可以改变 table.getTableHeader().setResizingAllowed(false); // 设置JTable每个表头的大小不可以改变 makeFace(table); // 设置JTable 颜色 table.setVisible(true); }
public RemoveTeacherTimetable() { frame = new JFrame("RemoveTeacherTimetable"); frame.setContentPane(panel); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.setVisible(true); tableView.setVisible( false); // hiding the unnecessary part before it is used, which makes the outlook more // pretty afterDeletion.setVisible(false); panelWidth = afterDeletion.getWidth() + 40; frame.pack(); 退出Button.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { RemoveModule removeModule = new RemoveModule(); frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); } }); 删除Button.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { DefaultTableModel tableModel; tableModel = new DefaultTableModel(); createTableModel(tableModel); tableView.setModel(tableModel); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog( null, "failed to load the driver function", "ERROR", JOptionPane.ERROR_MESSAGE); } try { Connection con = DriverManager.getConnection( Administrator.URL, Administrator.USER, Administrator.PASSWORD); Statement st = con.createStatement(); String Ayear = ayear.getText(); String Semester = semester.getText(); String T_id = t_id.getText(); String C_id = c_id.getText(); String query = "DELETE FROM tc WHERE Ayear = '" + Ayear + "' AND Semester = '" + Semester + "' AND T_id = '" + T_id + "' AND C_id = '" + C_id + "'"; if (!Ayear.isEmpty() && !Semester.isEmpty() && !T_id.isEmpty() && !C_id.isEmpty()) st.executeUpdate(query); ResultSet rs = st.executeQuery("SELECT * FROM tc"); while (rs.next()) { Vector<java.io.Serializable> vcRows = new Vector<>(); vcRows.addElement(rs.getString(1)); vcRows.addElement(rs.getInt(2)); vcRows.addElement(rs.getString(3)); vcRows.addElement(rs.getString(4)); vcRows.addElement(rs.getString(5)); tableModel.addRow(vcRows); } rs.close(); st.close(); con.close(); tableView.setAutoResizeMode( JTable .AUTO_RESIZE_ALL_COLUMNS); // adjust the panel according to the table's // current height, and set them visible panelHeight = tableView.getRowHeight() * tableView.getRowCount() + 50; afterDeletion.setPreferredSize(new Dimension(panelWidth, panelHeight)); afterDeletion.setVisible(true); tableView.setVisible(true); frame.pack(); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog( null, "failed to excute the SQL statement\n" + ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE); } } private void createTableModel(DefaultTableModel tableModel) { tableModel.addColumn("Ayear"); tableModel.addColumn("Semester"); tableModel.addColumn("T_id"); tableModel.addColumn("C_id"); tableModel.addColumn("Rating"); } }); }
public QueryTeacher() { frame = new JFrame("QueryTeacher"); frame.setContentPane(panel); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.setVisible(true); tableView.setVisible(false); afterInsert.setVisible(false); panelWidth = afterInsert.getWidth(); frame.pack(); 退出Button.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new QueryModule(); frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); } }); 查询Button.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { DefaultTableModel tableModel = new DefaultTableModel(); createTableModel(tableModel); tableView.setModel(tableModel); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog( null, "failed to load the driver function/n" + ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE); } try { Connection con = DriverManager.getConnection( Administrator.URL, Administrator.USER, Administrator.PASSWORD); Statement st = con.createStatement(); String T_id = t_id.getText(); String T_name = t_name.getText(); String T_sex = t_sex.getText(); String T_birth = t_birth.getText(); String T_prov = t_prov.getText(); String T_region = t_region.getText(); String Dept_id = dept_id.getText(); String Col_id = col_id.getText(); String Prof = prof.getText(); String Sal = sal.getText(); String query = "SELECT * FROM teacher where 1 = 1"; if (!T_id.isEmpty() && T_id != "") query += "and T_id like '%" + T_id + "%'"; if (!T_name.isEmpty() && T_name != "") query += "and T_name like '%" + T_name + "%'"; if (!T_sex.isEmpty() && T_sex != "") query += "and T_sex like '%" + T_sex + "%'"; if (!T_birth.isEmpty() && T_birth != "") query += "and T_birth like '%" + T_birth + "%'"; if (!T_prov.isEmpty() && T_prov != "") query += "and T_prov like '%" + T_prov + "%'"; if (!T_region.isEmpty() && T_region != "") query += "and T_region like '%" + T_region + "%'"; if (!Dept_id.isEmpty() && Dept_id != "") query += "and Dept_id like '%" + Dept_id + "%'"; if (!Col_id.isEmpty() && Col_id != "") query += "and Col_id like '%" + Col_id + "%'"; if (!Prof.isEmpty() && Prof != "") query += "and Prof like '%" + Prof + "%'"; if (!Sal.isEmpty() && Sal != "") query += "and Sal like '%" + Sal + "%'"; ResultSet rs = st.executeQuery(query); while (rs.next()) { Vector<java.io.Serializable> vcRows = new Vector<>(); vcRows.addElement(rs.getString(1)); vcRows.addElement(rs.getString(2)); vcRows.addElement(rs.getString(3)); vcRows.addElement(rs.getDate(4)); vcRows.addElement(rs.getString(5)); vcRows.addElement(rs.getString(6)); vcRows.addElement(rs.getString(7)); vcRows.addElement(rs.getString(8)); vcRows.addElement(rs.getString(9)); vcRows.addElement(rs.getInt(10)); tableModel.addRow(vcRows); } rs.close(); st.close(); con.close(); tableView.setAutoResizeMode( JTable .AUTO_RESIZE_ALL_COLUMNS); // adjust the panel according to the table's // current height, and set them visible panelHeight = tableView.getRowHeight() * tableView.getRowCount() + 50; afterInsert.setPreferredSize(new Dimension((int) (panelWidth * 1.4), panelHeight)); afterInsert.setVisible(true); tableView.setVisible(true); frame.pack(); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog( null, "failed to excute the SQL statement\n" + ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE); } } private void createTableModel(DefaultTableModel tableModel) { tableModel.addColumn("T_id"); tableModel.addColumn("T_name"); tableModel.addColumn("T_sex"); tableModel.addColumn("T_birth"); tableModel.addColumn("T_prov"); tableModel.addColumn("T_region"); tableModel.addColumn("Dept_id"); tableModel.addColumn("Col_id"); tableModel.addColumn("Prof"); tableModel.addColumn("Sal"); } }); }
private void updateTable() { dataExchange.transferString("updateTable"); listProduct = new ArrayList<ProductEntity>(); int size = dataExchange.acceptInt(); for (int i = 0; i < size; i++) { listProduct.add(dataExchange.acceptProductEntity()); } listGroup = new ArrayList<GroupEntity>(); size = dataExchange.acceptInt(); for (int i = 0; i < size; i++) { listGroup.add(dataExchange.acceptGroupEntity()); } listSubGroup = new ArrayList<SubGroupEntity>(); size = dataExchange.acceptInt(); for (int i = 0; i < size; i++) { listSubGroup.add(dataExchange.acceptSubGroupEntity()); } listProductTable = new ArrayList<ProductTableEntity>(); for (int i = 0; i < listProduct.size(); i++) { ProductTableEntity entity = new ProductTableEntity(); for (int j = 0; j < listGroup.size(); j++) { if (listProduct.get(i).getGroupId() == listGroup.get(j).getGroupId()) { entity.setGroupName(listGroup.get(j).getGroupName()); break; } } for (int j = 0; j < listSubGroup.size(); j++) { if (listProduct.get(i).getSubGroupId() == listSubGroup.get(j).getSubGroupId()) { entity.setSubGroupName(listSubGroup.get(j).getSubGroupName()); break; } } entity.setProductId(i + 1); entity.setProductName(listProduct.get(i).getProductName()); entity.setAmount(listProduct.get(i).getAmount()); entity.setPrice(listProduct.get(i).getPrice()); listProductTable.add(entity); } productTableModel.removeIsAll(); for (int i = 0; i < listProductTable.size(); i++) { String[] table = { String.valueOf(listProductTable.get(i).getProductId()), listProductTable.get(i).getProductName(), listProductTable.get(i).getGroupName(), listProductTable.get(i).getSubGroupName(), String.valueOf(listProductTable.get(i).getAmount()), String.valueOf(listProductTable.get(i).getPrice()) }; productTableModel.addDate(table); } table.setVisible(false); table.setVisible(true); }
public UpdateUsercode() { frame = new JFrame("UpdateUsercode"); frame.setContentPane(panel); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.setVisible(true); tableView.setVisible( false); // hiding the unnecessary part before it is used, which makes the outlook more // pretty afterUpdate.setVisible(false); panelWidth = afterUpdate.getWidth() + 40; frame.pack(); 退出Button.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); } }); 更改Button.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { DefaultTableModel tableModel = new DefaultTableModel(); createTableModel(tableModel); tableView.setModel(tableModel); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog( null, "failed to load the driver function", "ERROR", JOptionPane.ERROR_MESSAGE); } try { Connection con = DriverManager.getConnection( Administrator.URL, Administrator.USER, Administrator.PASSWORD); Statement st = con.createStatement(); String old_U_id = old_u_id.getText(); String U_id = u_id.getText(); String U_name = u_name.getText(); String Password = password.getText(); String Privilege = privilege.getText(); String query = "UPDATE usercode SET U_id = '" + U_id + "', U_name = '" + U_name + "', Password = '******', Privilege = '" + Privilege + "' WHERE U_id = '" + old_U_id + "'"; if (!old_U_id.isEmpty() && !U_id.isEmpty() && !U_name.isEmpty() && !Password.isEmpty() && !Privilege.isEmpty()) st.executeUpdate(query); ResultSet rs = st.executeQuery("SELECT * FROM usercode"); while (rs.next()) { Vector<String> vcRows = new Vector<>(); vcRows.addElement(rs.getString(1)); vcRows.addElement(rs.getString(2)); vcRows.addElement(rs.getString(3)); vcRows.addElement(rs.getString(4)); tableModel.addRow(vcRows); } rs.close(); st.close(); con.close(); tableView.setAutoResizeMode( JTable .AUTO_RESIZE_ALL_COLUMNS); // adjust the panel according to the table's // current height, and set them visible panelHeight = tableView.getRowHeight() * tableView.getRowCount() + 50; afterUpdate.setPreferredSize(new Dimension(panelWidth, panelHeight)); afterUpdate.setVisible(true); tableView.setVisible(true); frame.pack(); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog( null, "failed to excute the SQL statement\n" + ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE); } } private void createTableModel(DefaultTableModel tableModel) { tableModel.addColumn("U_id"); tableModel.addColumn("U_name"); tableModel.addColumn("Password"); tableModel.addColumn("Privilege"); } }); }