|
- 1://一.新建一表格視圖(MVC的View)
- JTable JTableView = new JTable();
- 2://二.新建一表格範本(MVC的Model)
- DefaultTableModel JTableModel = new DefaultTableModel();
- 3://三.新建createTableModel()方法
- private void createTableModel() {
- // JTableModel.setColumnCount(0); //清空表格範本內所有的列
- // JTableModel.setRowCount(0); //清空表格範本內所有的行
- // 在表格範本中加入列(加標題)
- JTableModel.addColumn("學號");
- JTableModel.addColumn("姓名");
- JTableModel.addColumn("性別");
- JTableModel.addColumn("成績");
- 4: //四.在初始化元件的方法中調用createTableModel()方法
- createTableModel();
- 5: //五.在表格視圖中設置要應用的表格範本
- JTableView.setModel(JTableModel);
- 6: // 連接資料庫第1種方法,直接寫代碼
- //1.載入驅動程式
- try {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println("驅動程式載入失敗!");
- }
- try {
- //2.建立資料庫連接
- Connection con = DriverManager.getConnection("jdbc:odbc:Lesson12");
- //3.建立Statement語句執行物件
- Statement st = con.createStatement();
- //4.建立ResultSet結果集,執行SQL命令
- ResultSet rs = st.executeQuery("select * from StuScore");
- // Vector vcRows = new Vector(); //error 每行顯示的都是第一條記錄
- //在表格範本中加入行(將表內記錄通過迴圈一行行添加到表格範本中去)
- while (rs.next())
- {
- Vector vcRows = new Vector();
- vcRows.addElement(rs.getInt(1));
- vcRows.addElement(rs.getString(2));
- vcRows.addElement(rs.getString(3));
- vcRows.addElement(rs.getFloat(4));
- // JTableModel.addRow(rs.getInt(1)); //error
- // JTableModel.addRow(rs.getString(2)); //error
- JTableModel.addRow(vcRows); //添加一行記錄到表格範本中
- }
- //5.關閉連接
- rs.close();
- st.close();
- con.close();
- // DBConnection.closeStmtAndCon(); //關閉相關連接
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println("資料庫連接失敗!");
- }
- =========完整的例子================================
- public class NewAssignDialog extends javax.swing.JDialog {
- org.htc.util.MyQuery mq = null;
- private int rowCount = 0;
- DefaultTableModel dTableModel = null;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JTable jTable1;
- /** Creates new form NewZujianDialog */
- public NewAssignDialog(java.awt.Frame parent, boolean modal) {
- super(parent, "臨牌發放", modal);
- //this.setLocation(120, 100);//視窗的位置
- setAutoCenter(true);//視窗的位置
- tiaomaTextField.setFocusable(true);
- jScrollPane1 = new javax.swing.JScrollPane();
- jTable1 = new javax.swing.JTable();
- jScrollPane1.setName("jScrollPane1"); // NOI18N
- jTable1.setAutoCreateRowSorter(true);
- jTable1.setModel(new javax.swing.table.DefaultTableModel(
- new Object [][] {
- {null, null, null, null},
- {null, null, null, null},
- {null, null, null, null},
- {null, null, null, null}
- },
- new String [] {
- "Title 1", "Title 2", "Title 3", "Title 4"
- }
- ));
- jTable1.setName("jTable1"); // NOI18N
- jScrollPane1.setViewportView(jTable1);
- dTableModel = this.getDefaultTableModel();
- jTable1.setModel(dTableModel);
- //禁止移動更改列的位置
- jTable1.getTableHeader().setReorderingAllowed(false);
- }
- //JLabel -start
- private DefaultTableModel createTableModel() {
- DefaultTableModel JTableModel = new DefaultTableModel();
- // JTableModel.setColumnCount(0); //清空表格範本內所有的列
- // JTableModel.setRowCount(0); //清空表格範本內所有的行
- // 在表格範本中加入列(加標題)
- JTableModel.addColumn("ID");
- JTableModel.addColumn("姓名");
- JTableModel.addColumn("性別");
- JTableModel.addColumn("成績");
- return JTableModel;
- }
- public DefaultTableModel getDefaultTableModel() {
- DefaultTableModel JTableModel = new DefaultTableModel() {
- public boolean isCellEditable(int row, int column) {//設置不可編輯
- return false;
- }
- };
- int pagesize = 0;
- mq = new org.htc.util.MyQuery();
- mq.setPageSize(0);//頁面大小,顯示的行數
- List list = new AssignAction().query(mq);
- rowCount = list.size();
- for (int i = 0; i < list.size(); i++) {
- DesAssignType dzt = (DesAssignType) list.get(i);
- Vector vcRows = new Vector();
- vcRows.addElement(dzt.getId());
- vcRows.addElement(dzt.getTiaoMa());
- vcRows.addElement(dzt.getId());
- vcRows.addElement(dzt.getTiaoMa());
-
- JTableModel.addRow(vcRows); //添加一行記錄到表格範本中
- }
- return JTableModel;
- }
- public DefaultTableModel addRowDate(DefaultTableModel dtm) {
- if (dtm == null) {
- dtm = this.createTableModel();
- }
- Vector vcRows = new Vector();
- vcRows.addElement("1");
- vcRows.addElement("2");
- vcRows.addElement("3");
- vcRows.addElement("4");
- dtm.addRow(vcRows); //添加一行記錄到表格範本中
- jTable1.setModel(dtm);
- return dtm;
- }
- //JLabel -end
- //選中某一行
- private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- // 取得使用者所選的多行
- int selectRows = jTable1.getSelectedRows().length;// 取得使用者所選行的行數
- DefaultTableModel tableModel = (DefaultTableModel) jTable1.getModel();
- if (selectRows > 1) {
- int[] selRowIndexs = jTable1.getSelectedRows();// 使用者所選行的序列
- for (int i = 0; i < selRowIndexs.length; i++) {
- // 用tableModel.getValueAt(row, column)取儲存格資料
- int selRowIndex = selRowIndexs[i];
- selRowIndex = selRowIndex-i;
- //選擇table第一格的欄位值
複製代碼 |
|