如何在一个JTable中双击内容到另一个框架中的另一个JTable?

时间:2015-06-29 10:41:02

标签: java swing jtable

我正在设计像发票一样的框架。在这里我有一个JTable2,我需要JTable1的数据,当我双击它在同一个包中的不同帧中时。我如何获取数据?

String sql = "select * from table1 where Bill_No like'"+jTextField2.getText()+"'";
try{
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();
        TableModel original = jTable2.getModel();
        DefaultTableModel model = new DefaultTableModel(jTable2.getSelectedRowCount(), original.getColumnCount());
        for (int col = 0; col < original.getColumnCount(); col++) {
        model.addColumn(original.getColumnName(col));
    }
        int[] selectedRows = jTable2.getSelectedRows();
        for (int targetRow = 0; targetRow < selectedRows.length; targetRow++) {
        int row = selectedRows[targetRow];
        int modelRow = jTable1.convertRowIndexToModel(row);
        for (int col = 0; col < original.getColumnCount(); col++) {
        model.setValueAt(original.getValueAt(modelRow, col), targetRow, col);
    }
}

        NewJFrame2 n = new NewJFrame2();
        n.setVisible(true);           
        int i=0; 
        while(rs.next()) {
        Object bno = rs.getString("Bill No");
        Object bamount = rs.getString("Bill Amount");
        Object btds = rs.getString("TDS");
        Object btax = rs.getString("Tax");
        Object bpayable = rs.getString("Payable");      

        jTable1.getModel().setValueAt(bno,i, 0 );
        jTable1.getModel().setValueAt(bamount, i, 1);
        jTable1.getModel().setValueAt(btds, i, 2);
        jTable1.getModel().setValueAt(btax, i, 3);
        jTable1.getModel().setValueAt(bpayable, i, 4);              
        System.out.println(i);
        i++;
        }
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }

1 个答案:

答案 0 :(得分:0)

你不应该使用setValue,最好使用表模型,你将有一个扩展控件。

我做了一个例子,如果你有类似的东西:

MyTableModel model1 = new MyTableModel();
MyTableModel model2 = new MyTableModel();
JTable table1 = new JTable(model1);
JTable table2 = new JTable(model2);

使用表模型类: 公共类MyTableModel扩展AbstractTableModel {

    private String[] columnNames = { "Col 1", "Col 2", "Col 3"};
    private Vector<Vector<String>> data;//I am using String because I don't know what you need

    public int getColumnCount() {
      return columnNames.length;
    }

    public Vector<Vector<String>> getData(){
        return data;
    }

    public void setData(Vector<Vector<String>> data){
        this.data=data;
        fireTableDataChanged();
    }

    public int getRowCount() {
      return data.size();
    }

    public String getColumnName(int col) {
      return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
      return data.elementAt(row).elementAt(col);
    }
    public Class getColumnClass(int c) {
      return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col) {
        return false;
    }

    public void setValueAt(Object value, int row, int col) {
        data.elementAt(row).set(col,value.toString());
      fireTableCellUpdated(row, col);
    }

  }

然后你可以让一个鼠标监听器,让你修改表模型的内容:

table1.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me) {
                JTable table =(JTable) me.getSource();
                Point p = me.getPoint();
                int row = table.rowAtPoint(p);
                if (me.getClickCount() == 2) {
                    Vector<Vector<String>> data1=model1.getData();
                    Vector<String> line=data.elementAt(row);
                    //remove from first table
                    data1.remove(row);
                    model1.setData(data);
                    //add it to the second one (at the end)
                    Vector<Vector<String>> data2 = model2.getData();
                    data2.add(line);
                    model2.setData(data2);
                }
            }
        });