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


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

    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) {
      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
                    //add it to the second one (at the end)
                    Vector<Vector<String>> data2 = model2.getData();

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()+"'";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            JFrame NewJFrame2 = new NewJFrame2();
            int i=0; 
            while( {
            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);              
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);