| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 644 人关注过本帖
标题:跪求:JTable的问题
只看楼主 加入收藏
19860815abc
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-9-6
收藏
 问题点数:0 回复次数:0 
跪求:JTable的问题
我用JTable写了一个表格,现在,我已经选中一些表格,但是我想通过某个类或方法,一次性将表格中的数据删除掉,怎么做啊??请高手高手我,跪求了!!!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import *;
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 
2007-12-19 03:28
快速回复:跪求:JTable的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017390 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved