跪求一个答案:
   新建了一个只有一行的JTable对象.想实现在每一行的第一个单元格单击时,table添加一行空行...问了几天了...没人理....惨啊....郁闷啊没分了..只能给40......

解决方案 »

  1.   

    package addbook;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.event.*;
    import java.util.*;
    public class AddbookFrame extends JFrame{
    JPanel content;
    JLabel addLab=new JLabel("      添加新书目",new ImageIcon("bookbig.gif"),JLabel.LEFT);
    JButton addBtn=new JButton("添  加");
    JButton cancelBtn=new JButton("取  消");
    String[] title={"索书号","书名","作者","出版社","出版日期","价格","所属类别"};
        JScrollPane scroll=null;
        JTable table;
        DefaultTableModel tm=new DefaultTableModel(title,1);
        Vector vector=new Vector();
        Connection connection=null;
        Statement statement=null;
        String driverName="sun.jdbc.odbc.JdbcOdbcDriver";
    String user="system";
    String passWord="ericdb";
        String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=ericdb.mdb";
        String sqlStatement=new String();
        JComboBox comboBox;
        public AddbookFrame()
        {
         super();
    setTitle("添加新书");
    this.setResizable(false);
    setIconImage(Toolkit.getDefaultToolkit().createImage("bookbig.gif"));
    Dimension screanSize=Toolkit.getDefaultToolkit().getScreenSize();
    setSize(860,440);
    Dimension frameSize=this.getSize();
    setLocation((screanSize.width-frameSize.width)/2,
                     (screanSize.height-frameSize.height)/2);
    content=(JPanel)this.getContentPane();
    content.setLayout(null);
    addLab.setBounds(30,10,600,50);
        sqlStatement="select * from KIND";
    try
    {
    Class.forName(driverName);
    }
    catch(java.lang.ClassNotFoundException ex){
    }
    try
    {
    connection=DriverManager.getConnection(url,user,passWord);
    statement=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
    ResultSet.CONCUR_UPDATABLE);
    ResultSet res=statement.executeQuery(sqlStatement);
    while(res.next())
    vector.addElement(res.getString("KIND_NAME"));
    res.close();
             }
    catch(SQLException ex)
    {
    System.out.println(ex.getErrorCode());
    System.out.println(ex.getMessage());
    System.out.println(ex.getSQLState());         
    }
    *********************************************************************** 
    在该table对象的每一行的第一个单元格单击时,table添加一行空行
    table=new JTable(tm);
    TableColumn Column = table.getColumnModel().getColumn(6);
             comboBox=new JComboBox(vector);
             Column.setCellEditor(new DefaultCellEditor(comboBox));
    ((DefaultCellEditor) table.getDefaultEditor(Object.class)).setClickCountToStart(1);
       ***********************************************************************************   
    scroll=new JScrollPane(table);
    scroll.setBounds(20,80,820,240);
    addBtn.setBounds(50,340,160,30);
    addBtn.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){

    try{
    for(int i=0;i<table.getRowCount();i++){
    String updateSql="insert into BOOK values('"+
    table.getValueAt(i,0)+"','"+table.getValueAt(i,1)+"','"+
    table.getValueAt(i,2)+"','"+table.getValueAt(i,3)+"','"+
    table.getValueAt(i,4)+"','"+table.getValueAt(i,5)+"','"+
    table.getValueAt(i,6)+"')";
                statement.executeUpdate(updateSql);
               }
            JOptionPane msg=new JOptionPane();
              msg.showMessageDialog(AddbookFrame.this,"新书目添加成功","添加成功",1);
    }
    catch(SQLException ex)
    {
    System.out.println(ex.getErrorCode());
    System.out.println(ex.getMessage());
    System.out.println(ex.getSQLState());         
    }

    try{
                if(statement!=null)statement.close();
                }
                catch(SQLException ex)
                {
                }
         try{
                if(statement!=null)connection.close();
                }
                catch(SQLException ex)
                {
                }
    }
    });
        cancelBtn.setBounds(240,340,160,30);
        content.add(cancelBtn);
    content.add(addLab);
    content.add(scroll);
    content.add(addBtn);
                     
        }     public static void main(String[] args)
    {
    AddbookFrame frame=new AddbookFrame();
    frame.addWindowListener(new WindowAdapter(){
    public void WindowClosing(WindowEvent e){
    System.exit(0);
    }
    });
    frame.show();
    }}
      

  2.   

    实现接口MouseListener判断单击信息,然后再做你所想做的事! -_-
      

  3.   

    回复人: hifan(Hifan On Line) ( ) 信誉:100  2005-6-12 18:53:18  得分: 0  
     
     
       
    实现接口MouseListener判断单击信息,然后再做你所想做的事! -_-  
     
    尝试过了...但是不行...不知道为什么