我的意思是在创建JTable(String[] Data[][])之后如何通过mouseevent 或者keyevent来添加或者删除 rows 和cols..

解决方案 »

  1.   

    use TableColumnModel instead
      

  2.   

    哇,看在200分的份上呵呵
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import java.util.*;public class Test extends JFrame {
    private int rows=3, cols=5;
    private Object[] rowData = new Object[cols]; private DefaultTableModel model = new DefaultTableModel();
    private JTable table = new JTable(model); public Test() {
    for(int c=0; c < cols; ++c)
    model.addColumn("Column " + Integer.toString(c)); for(int r=0; r < rows; ++r) {
    for(int c=0; c < cols; ++c) {
    rowData[c] = "(" + r + "," + c + ")";
    }
    model.addRow(rowData);
    }
    getContentPane().add(new JScrollPane(table),
     BorderLayout.CENTER);
    getContentPane().add(new ControlPanel(),
     BorderLayout.NORTH);
    }
    public static void main(String args[]) {
    Test test=new Test();
    test.setBounds(100,100,300,300);
    test.show();
    }
    class ControlPanel extends JPanel {
    private JButton rowButton = new JButton("Add Row"),
         colButton = new JButton("Add Column"); public ControlPanel() {
    add(rowButton);
    add(colButton); rowButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    int rowCount = model.getRowCount();
    int colCount = model.getColumnCount(); if(colCount > rowData.length)
    rowData = new Object[colCount]; for(int c=0; c < colCount; ++c) {
    rowData[c] = "(" + rowCount + "," + 
           c + ")";
    }
    model.addRow(rowData);
    }
    });
    colButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    int colCount = model.getColumnCount();
    model.addColumn("Column " + colCount);
    table.sizeColumnsToFit(-1);
    }
    });
    }
    }
    }