当前位置: 代码迷 >> Java相关 >> 跪求:JTable的问题
  详细解决方案

跪求:JTable的问题

热度:322   发布时间:2007-12-19 03:28:01.0
跪求:JTable的问题
我用JTable写了一个表格,现在,我已经选中一些表格,但是我想通过某个类或方法,一次性将表格中的数据删除掉,怎么做啊??请高手高手我,跪求了!!!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import javax.swing.border.*;
import javax.swing.table.*;
import javax.swing.event.*;
import c.*;
public class jc_table extends AbstractTableModel implements ActionListener,ListSelectionListener
{
     JFrame f;
     Container contentPane;
     static JTable table;
     JButton b;
     JButton b1;
     JButton b2;
     JButton b3;
     JButton b4;
     JButton b5;
     int n=50,m=7;
     String[][] p = new String[50][8];
    String[] name = {"索书号","书名","编者","出版社","版本","出版日期","计划订购数量","课程类型"};
     public jc_table()
     {

          f=new JFrame();
          contentPane=f.getContentPane();
          JPanel p1 = new JPanel();
          p1.setLayout(new GridLayout(1,5));
          p1.setSize(30,560);
          ListSelectionModel selectionMode=null;
          table=new JTable(p,name);
          table.getTableHeader().setBackground(Color.blue); //设置表格标题背景色为蓝色
          table.getTableHeader().setForeground(Color.orange); //设置表格标题前景色为橙色
          table.setPreferredScrollableViewportSize(new Dimension(740,560));
          table.setCellSelectionEnabled(true);
          selectionMode=table.getSelectionModel();//取得table的ListSelectionModel.
          selectionMode.addListSelectionListener(this);
          selectionMode.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
          table.revalidate();
          b=new JButton("保存");
          b.addActionListener(this);
          b1=new JButton("插入");
          b1.addActionListener(this);
          b2=new JButton("删除");
          b2.addActionListener(this);
          b3=new JButton("查询");
          b3.addActionListener(this);
          b4=new JButton("新建录入表");
          b4.addActionListener(this);
          b5=new JButton("主界面");
          b5.addActionListener(this);

          p1.add(b);
          p1.add(b1);
          p1.add(b2);
          p1.add(b3);
          p1.add(b4);
          p1.add(b5);
          contentPane.add(new JScrollPane(table), BorderLayout.CENTER);
          contentPane.add(p1, BorderLayout.SOUTH);
         

          f.pack();
          f.setBounds(200,200,740,560);
          f.setVisible(true);
         


     }

     public void valueChanged(ListSelectionEvent el){
   String tempString="";
//JTable的getSelectedRows()与getSelectedColumns()方法会返回已选取表格cell的index Array数据.
   int[] rows=table.getSelectedRows();
   int[] columns=table.getSelectedColumns();

     for (int i=0;i<rows.length;i++){
       for (int j=0;j<columns.length;j++)
         tempString = tempString+" "+(String)table.getValueAt(rows[i], columns[j]);      
     }
    //tabel.setText("你选取:"+tempString);

}

     public int getColumnCount() {
         return n;
     }

     public int getRowCount() {
         return m;
     }

     public String getColumnName(int col) {
         return name[col];
     }

     public Object getValueAt(int row, int col) {
         return p[row][col];
     }

     public void actionPerformed(ActionEvent e)
     {

         String cmd=e.getActionCommand();
         String t1,t2,t3,t4,t5,t6,t7,t8;
       // String[] t;


          if(cmd.equals("确定"))
          {
              table.updateUI();
              if(table.isEditing()){
         int   row   =   table.getEditingRow();
         int   col   =   table.getEditingColumn();
         table.getCellEditor(row,col).stopCellEditing();
   }

     for(int i=0;i<50;i++){

       t1 = table.getValueAt(i, 0).toString();
        t2 = table.getValueAt(i, 1).toString();
        t3 = table.getValueAt(i, 2).toString();
        t4 = table.getValueAt(i, 3).toString();
        t5 = table.getValueAt(i, 4).toString();
        t6 = table.getValueAt(i, 5).toString();
        t7 = table.getValueAt(i, 6).toString();
        t8 = table.getValueAt(i, 6).toString();
         try {

             connect c=new connect();
             Connection con=c.conn;
             Statement stmt = con.createStatement();
             stmt.executeUpdate("insert into LogPlan(ISBN,LBookname,LEditor,LPublish,LEdition,LPublishTime,LAmount,LCourseType) values('" + t1 + "','" + t2 +
                                "','" + t3 + "','" + t4 + "','" + t5 + "','" + t6 +
                               "','" + t7 + "','"+t8+"') ");

         } catch (java.sql.SQLException ex) {
             System.out.println("SQL错误!" + ex);

         } catch (Exception em) {
             em.printStackTrace();
         }
     }
   }
   if(cmd.equals("新建录入表")){
       new jc_table();
   }
   if(cmd.equals("删除")){   
   }
   if(cmd.equals("插入")){      
   }    }

public static void main(String[] args){
         new jc_table();
   } }
请高手继续写代码啊,请指教啊!!
搜索更多相关的解决方案: JTable  

----------------解决方案--------------------------------------------------------
  相关解决方案