Reputation: 95
I am transferring data from a jtable in frame1 to another jtable in frame2. the problem is, every time i select a row from my first table and press the button(to transfer it to the other table) another frame pops up and the data is fed in the first row. I want all the selected data to be in the same table.
I know ive written something wrong in my code but i doesnt know a right solution to it ! here is my code
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
int row = jTable2.getSelectedRow();
String a1=jTable2.getModel().getValueAt(row, 1).toString();
String a2=jTable2.getModel().getValueAt(row, 4).toString();
String a3=jTable2.getModel().getValueAt(row, 21).toString();
String a4=jTable2.getModel().getValueAt(row, 5).toString();
String a5=jTable2.getModel().getValueAt(row, 22).toString();
NewJFrame2 fr2 = new NewJFrame2();
fr2.gencode(a1, a2, a3, a4, a5);
//this.dispose();
fr2.setVisible(true);
}
and this in another frame
public NewJFrame2() {
initComponents();
}
int i=0;
public void gencode(String a1, String a2, String a3, String a4, String a5){
System.out.print(i);
i++;
jTable1.setValueAt(a1, i, 0);
jTable1.setValueAt(a2, i, 1);
jTable1.setValueAt(a3, i, 2);
jTable1.setValueAt(a4, i, 3);
jTable1.setValueAt(a5, i, 4);
}
Upvotes: 0
Views: 795
Reputation: 1046
NewJFrame2 fr2=null; //class level variable
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
int row = jTable2.getSelectedRow();
String a1=jTable2.getModel().getValueAt(row, 1).toString();
String a2=jTable2.getModel().getValueAt(row, 4).toString();
String a3=jTable2.getModel().getValueAt(row, 21).toString();
String a4=jTable2.getModel().getValueAt(row, 5).toString();
String a5=jTable2.getModel().getValueAt(row, 22).toString();
//create instance when not initiated
if(fr1==null){
fr2 = new NewJFrame2();
}
fr2.gencode(a1, a2, a3, a4, a5);
//this.dispose();
fr2.setVisible(true);
}
Make fr2 as class level variable and create instance when not initiated
Upvotes: 1
Reputation: 9900
problem is you are creating new frame inside button click event
NewJFrame2 fr2 = new NewJFrame2(); //here is the problem
in your frame one declare a instance variable frame2
class One{
NewJFrame2 fr2;
}
and initialize it when you need but only one time and in the action method use
if(fr2==null){
fr2=new NewJFrame2(); //initialize if it's null
}
fr2.gencode(a1, a2, a3, a4, a5);
Upvotes: 1