Copy selected data from a jtable in frame1 to another table in frame2




(3)

I have a JTable2 in frame1 and JTable1 in frame2. I want to copy and send selected data from table2 to table1. how do i do it ?

private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {                                      
String sql = "select * from table1 where Bill_No like '"+jTextField2.getText()+"'";
        try{
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            jTable2.setModel(DbUtils.resultSetToTableModel(rs));
            JFrame NewJFrame2 = new NewJFrame2();
            NewJFrame2.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);
        }
    }             

Answers

Start by having a look at How to Use Tables.

If you want to "copy" the selected data, then you will need to know what rows are selected, see JTable#getSelectedRows.

You're making life difficult for yourself using DbUtils as you've lost the ability to just transfer the objects from one model to another.

The basic idea would be to copy the values from the original table into a new TableModel and pass that to the second window, something like

TableModel original = table.getModel();
DefaultTableModel model = new DefaultTableModel(table.getSelectedRowCount(), original.getColumnCount());
for (int col = 0; col < original.getColumnCount(); col++) {
    model.addColumn(original.getColumnName(col));
}

int[] selectedRows = table.getSelectedRows();
for (int targetRow = 0; targetRow < selectedRows.length; targetRow++) {
    int row = selectedRows[targetRow];
    int modelRow = table.convertRowIndexToModel(row);
    for (int col = 0; col < original.getColumnCount(); col++) {
        model.setValueAt(original.getValueAt(modelRow, col), targetRow, col);
    }
}

for example. Now you just need to pass model to the second window and apply it to the JTable contained within


How to pass contents on double click from one JTable to another JTable in a different frame?

You should not use setValue, it's better to work with the table model, you will have an extended control.

I made an example, If you have something like:

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

With the table model class: public class MyTableModel extends 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);
    }

  }

Then you can have a mouse listener to the table, that let you modify the content of the table models:

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);
                }
            }
        });

If you want to notify your JTable about changes of your data, use
tableModel.fireTableDataChanged()

From the documentation:

Notifies all listeners that all cell values in the table's rows may have changed. The number of rows may also have changed and the JTable should redraw the table from scratch. The structure of the table (as in the order of the columns) is assumed to be the same.