SilverRay SilverRay - 22 days ago 13
Java Question

Update jinternalframe table dynamically from another jinternalframe

I have two jinternalframes with the first one is for viewing and the other is for the data processing. The data table of the first jinternalframe is the same as the second jinternalframe. What I want is to make the first jinternalframe updates its jtable after I added data from second jinternalframe. Please see attached photos of the first jinternal frame and the second jinternalframe.
First Jinternalframe

Second jinternalframe

And this is the code snippet for the first frame:

table1 = new JTable();
table1.setModel(loadTable1());
scrollPane.setViewportView(table1);
}
public static DefaultTableModel loadTable1(){
data = new ModelDB();
tableModel1 = new DefaultTableModel();
tableModel1.addColumn("ID");
tableModel1.addColumn("Allocation");
tableModel1.addColumn("Shipment Date");
tableModel1.addColumn("Buyers");
tableModel1.addColumn("Design");

try{
List<String> id = data.getAllocation().get(0);
List<String> allocation = data.getAllocation().get(1);
List<String> shipdate = data.getAllocation().get(2);
List<String> buyers = data.getAllocation().get(3);
List<String> design = data.getAllocation().get(4);

for(int i=0;i<id.size();i++){
tableModel1.addRow(new Object[]{
id.get(i),allocation.get(i),shipdate.get(i),buyers.get(i),design.get(i)
});
}
}catch(Exception ex){}

return tableModel1;
}


And this is the code snippet for the second frame:

@Override
public void actionPerformed(ActionEvent evt){
data = new ModelDB();
String action = evt.getActionCommand();
String allocation = txtAllocation.getText().toUpperCase();
Date shipdate = (Date) shipDate.getModel().getValue();
String buyers = String.valueOf(model4.getCheckeds()).replaceAll("[\\[\\](){}]", "");
String design = String.valueOf(model.getCheckeds()).replaceAll("[\\[\\](){}]","");
String stylecode = String.valueOf(model2.getCheckeds()).replaceAll("[\\[\\](){}]", "");
String sizes = String.valueOf(model3.getCheckeds()).replaceAll("[\\[\\](){}]", "");
String pcspack = txtPcsPack.getText();
String season = txtSeason.getText().toUpperCase();
String orderqty = txtQtyOrder.getText();

if(action.equals("Add")){
btnSave.setText("Save");
btnSave.setActionCommand("Save");
btnCancel.setEnabled(true);
txtAllocation.setEnabled(true);
txtAllocation.grabFocus();
shipDate.setEnabled(true);
cmbBuyers.setEnabled(true);
cmbDesign.setEnabled(true);
cmbStyleCode.setEnabled(true);
cmbSize.setEnabled(true);
txtPcsPack.setEnabled(true);
txtSeason.setEnabled(true);
txtQtyOrder.setEnabled(true);
}
else if(action.equals("Save")){
if(allocation.isEmpty() || shipdate.equals("") || pcspack.isEmpty() || season.isEmpty() || season.isEmpty() || orderqty.isEmpty() || buyers.isEmpty()||
design.isEmpty() || stylecode.isEmpty() || sizes.isEmpty()){
message.showMessageDialog(null, "Nothing to save");
}
else{
try{
if(data.addAllocation(allocation, shipdate, buyers, design, stylecode, sizes, pcspack, season, orderqty) == true){
message.showMessageDialog(null, "Data have been successfully saved");
}
else{
message.showMessageDialog(null, "Failed to save");
}
}catch(Exception ex){
ex.printStackTrace();
}
table1.setModel(loadTable());
}
btnSave.setText("Add");
btnSave.setActionCommand("Add");
btnCancel.setEnabled(false);
txtAllocation.setEnabled(false);
shipDate.setEnabled(false);
cmbBuyers.setEnabled(false);
cmbDesign.setEnabled(false);
cmbStyleCode.setEnabled(false);
cmbSize.setEnabled(false);
txtPcsPack.setEnabled(false);
txtSeason.setEnabled(false);
txtQtyOrder.setEnabled(false);

txtAllocation.setText("");
model.removeChecks();
model2.removeChecks();
model3.removeChecks();
model4.removeChecks();
txtPcsPack.setText("");
txtSeason.setText("");
txtQtyOrder.setText("");
}

else if(action.equals("Cancel")){
btnSave.setText("Add");
btnSave.setActionCommand("Add");
btnCancel.setEnabled(false);
txtAllocation.setEnabled(false);
shipDate.setEnabled(false);
cmbBuyers.setEnabled(false);
cmbDesign.setEnabled(false);
cmbStyleCode.setEnabled(false);
cmbSize.setEnabled(false);
txtPcsPack.setEnabled(false);
txtSeason.setEnabled(false);
txtQtyOrder.setEnabled(false);

txtAllocation.setText("");
model.removeChecks();
model2.removeChecks();
model3.removeChecks();
model4.removeChecks();
txtPcsPack.setText("");
txtSeason.setText("");
txtQtyOrder.setText("");
}
}

Answer

The table of the first jinternalframe is the same as the second jinternalframe.

It can't be the same table because a Swing component can only have a single parent. So you must have two tables.

What I want is to make the first jinternalframe updates its jtable after I added data from second jinternalframe.

So maybe you are saying you want the "data" to be the same in both tables.

If this is the case then you can share the TableModel between the two tables. Then when the data is changed in one it is automatically updated in the other:

DefaultTableModel model = new DefaultTableModel(...);
JTable table1 = new JTable( model );
JTable table2 = new JTable( model );

If you want you can have different columns display in one of the table because you can remove a TableColumn from the TableColumnModel. Something like:

TableColumnModel tcm = table1.getColumnModel();
tcm.removeColumn( tcm.getColumn(...) ); // specify column number

or

table1.removeColumn( table1.getColumn(...) ); // specify column name