-1

我正在尝试实现 MS EXCEL 的求和功能,但没有办法从 JTable 单元格中获取值。你能建议我一些从单元格中获取值并将其用于我的功能的方法吗?

4

1 回答 1

1

例如,您如何获得价值 -

  String data[][] = {{"Value1", "Value2", "Value3"},{"Value4", "Value5", "Value6"},
  {"Value7", "Value8", "Value9"},{"Value10", "Value11", "Value12"}};

  String col[] = {"Column1", "Column2", "Column3"};  

  DefaultTableModel model = new DefaultTableModel(data, col);
  JTable table = new JTable(model);
  ...
  System.out.println(table.getModel().getValueAt(2, 2)); // row index and column index
  ...

它给 -

Value9

要从表中获取值,可以在匿名内部类中实现接口MouseListener,例如——

table.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
         JTable target = (JTable) e.getSource();

         int rowIndex = target.getSelectedRow();
         int columnIndex = target.getSelectedColumn();

        System.out.println(target.getModel().getValueAt(rowIndex, columnIndex));
     }
});

也可以看看:

于 2013-10-19T16:53:34.927 回答