-1

I am designing a frame like a invoice. Here I have a JTable2 to which i need data from JTable1 when i double click on it which is in a different frame in the same package. How do I get the data ?

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);
    }
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Anmol Khanna
  • 95
  • 2
  • 9
  • You should share what you've tried, some code that we can execute and see what is wrong. You should probably use mouse listener on the table to fire the event of changing the selected row content to another table model. – Bepo Jun 29 '15 at 10:46
  • @ Bepo This is how i can put data from one jtable to another in the same frame ! I am facing problem in sharing it in a jtable in another frame. jTable2.setValueAt(jTable1.getValueAt(0, 0).toString(), 0, 0); jTable2.setValueAt(jTable1.getValueAt(0, 1).toString(), 0, 1); jTable2.setValueAt(jTable1.getValueAt(0, 2).toString(), 0, 2); – Anmol Khanna Jun 29 '15 at 10:51
  • Na, it shouln't make a difference how many frames you have, and to which frame you send the data. As long you have the table modal, you can place it everywhere. – Mordechai Jun 29 '15 at 10:54
  • @MouseEvent you can see my code. what wrong am i doing ? i get a cannot find symbol JTable1 error – Anmol Khanna Jun 29 '15 at 10:58
  • You never created the other jtable (at-least, in your posted code), and I don't see in your code where you add the table to the frame. – Mordechai Jun 29 '15 at 11:02
  • This is frame 1 where i am trying to use table2 of frame2. – Anmol Khanna Jun 29 '15 at 11:06
  • Have a look at [Passing Information to a Method or a Constructor](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html) for information about how to pass information between classes – MadProgrammer Jun 29 '15 at 11:29
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) One (or possibly more) of those frames should probably be a `JDialog`. Then when modal it becomes simpler to update the table in the 'main' frame or dialog. – Andrew Thompson Jun 29 '15 at 12:35

1 Answers1

0

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);
                }
            }
        });
Bepo
  • 52
  • 8
  • Why go to all the extra work of creating a `TableModel` implementation when you've basically implemented a `DefaultTableModel` ... And `Vector`? It's a little out-of-date – MadProgrammer Jun 29 '15 at 11:25