用JTable的setValueAt(int iRow,int iCol)方法设定单元格数据
用getValueAt(int iRow,int iCol)得到单元格数据如还不能帮你,请再说详细点,在给你解答

解决方案 »

  1.   

    onefox(一品狐) 
    没有问题的
    为了学习~~~~
    我不惜一切代价了
      

  2.   

    我研究了一下jdk的demo里面的代码,其中有一个例子是讲如何选择一行或一列
    现在就应该能够确定某一个单元格了吧,对于其中的数据也应该可以选择出来吧import javax.swing.JTable;
    import javax.swing.ListSelectionModel;
    import javax.swing.event.ListSelectionListener;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.JScrollPane;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.awt.*;
    import java.awt.event.*;public class SimpleTableSelectionDemo extends JFrame {
        private boolean DEBUG = true;
        private boolean ALLOW_COLUMN_SELECTION = true;
        private boolean ALLOW_ROW_SELECTION = true;    public SimpleTableSelectionDemo() {
            super("SimpleTableSelectionDemo");        Object[][] data = {
                {"Mary", "Campione", 
                 "Snowboarding", new Integer(5), new Boolean(false)},
                {"Alison", "Huml", 
                 "Rowing", new Integer(3), new Boolean(true)},
                {"Kathy", "Walrath",
                 "Chasing toddlers", new Integer(2), new Boolean(false)},
                {"Sharon", "Zakhour",
                 "Speed reading", new Integer(20), new Boolean(true)},
                {"Angela", "Lih",
                 "", new Integer(4), new Boolean(false)}
            };        String[] columnNames = {"First Name", 
                                    "Last Name",
                                    "Sport",
                                    "# of Years",
                                    "Vegetarian"};        final JTable table = new JTable(data, columnNames);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            if (ALLOW_ROW_SELECTION) { // true by default
                ListSelectionModel rowSM = table.getSelectionModel();
                rowSM.addListSelectionListener(new ListSelectionListener() {
                    public void valueChanged(ListSelectionEvent e) {
                        //Ignore extra messages.
                        if (e.getValueIsAdjusting()) return;
                        
                        ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                        if (lsm.isSelectionEmpty()) {
                            System.out.println("No rows are selected.");
                        } else {
                            int selectedRow = lsm.getMinSelectionIndex();
                            System.out.println("Row " + selectedRow
                                               + " is now selected.");
                        }
                    }
                });
            } else {
                table.setRowSelectionAllowed(false);
            }        if (ALLOW_COLUMN_SELECTION) { // false by default
                if (ALLOW_ROW_SELECTION) {
                    //We allow both row and column selection, which
                    //implies that we *really* want to allow individual
                    //cell selection.
                    table.setCellSelectionEnabled(true);
                } 
                table.setColumnSelectionAllowed(true);
                ListSelectionModel colSM =
                    table.getColumnModel().getSelectionModel();
                colSM.addListSelectionListener(new ListSelectionListener() {
                    public void valueChanged(ListSelectionEvent e) {
                        //Ignore extra messages.
                        if (e.getValueIsAdjusting()) return;                    ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                        if (lsm.isSelectionEmpty()) {
                            System.out.println("No columns are selected.");
                        } else {
                            int selectedCol = lsm.getMinSelectionIndex();
                            System.out.println("Column " + selectedCol
                                               + " is now selected.");
                        }
                    }
                });
            }        if (DEBUG) {
                table.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        printDebugData(table);
                    }
                });
            }        //Create the scroll pane and add the table to it. 
            JScrollPane scrollPane = new JScrollPane(table);        //Add the scroll pane to this window.
            getContentPane().add(scrollPane, BorderLayout.CENTER);        addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }    private void printDebugData(JTable table) {
            int numRows = table.getRowCount();
            int numCols = table.getColumnCount();
            javax.swing.table.TableModel model = table.getModel();        System.out.println("Value of data: ");
            for (int i=0; i < numRows; i++) {
                System.out.print("    row " + i + ":");
                for (int j=0; j < numCols; j++) {
                    System.out.print("  " + model.getValueAt(i, j));
                }
                System.out.println();
            }
            System.out.println("--------------------------");
        }    public static void main(String[] args) {
            SimpleTableSelectionDemo frame = new SimpleTableSelectionDemo();
            frame.pack();
            frame.setVisible(true);
        }
    }
      

  3.   

    这个其实比较简单。
    直接做的话,先得到选定的行(jtable.getSelectedRow())
    然后就可以调用JTable.getValueAt()和JTable.setValueAt()来对表格中的数据进行读取、更新了。这个应该是最基本的做法,且自己可以在这基础上进行包装
    另外做法:看你的JTable的Model是自己写的还是用默认的了。
    反正不管怎么样都会有一个接口让你往Model里面set数据的,也有对应的取数据的方法。具体看你怎么作了
      

  4.   

    很对不起,我回学校后校园网一直段了,所以这么晚才回复你的问题其实很简单,起先我看错了。 我做了个简单的例子,下载位置在下面http://my.nbip.net/homepage/zhouB403/MyCode/TableEditDemo.exe双击一行记录,打开行编辑窗口
      

  5.   

    前几天我通过研究demo解决这个问题了
    不过我还是很谢谢你的帮助
    我也仔细看了你的代码,不错
    谢谢但是我还是有一个问题,我感觉JTable对鼠标的响应速度很忙,不是太灵敏,单击一行数据后要等一阵子才能得到所在表格的数据,或者要重新点击后才会出来,有没有比较好的解决方法?(实在是不好意思再麻烦你)
      

  6.   

    可能和我查询数据库有关系
    我每次得到表格中的一个数值后就要用这个数到数据库中查询相应的数据
    这个过程就比较慢了
    现在我用的是Access的数据库,这跟数据库有关系吗?