0

i am facing the problem while i am running my project. The situation is, i have a button which let me to print the output in the table. However, every time i click on the button, the table is appending rather than replacing the old value. For jtextarea i solved it using a simply way which is use jtextarea.settext rather thn jtextarea.append. This is how i passing the value in to the table DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); model.addRow(row); i declare row as vector. Can anyone tell me how to make it replace the value rather than append.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Aaron
  • 47
  • 1
  • 10
  • 1
    [`TableModel#setValueAt`](https://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html#setValueAt(java.lang.Object,%20int,%20int)). Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jan 07 '16 at 04:56
  • 1
    You could use a combination of [`DefaultTableModel#removeRow`](https://docs.oracle.com/javase/7/docs/api/javax/swing/table/DefaultTableModel.html#removeRow(int)) and [`DefaultTableModel#insertRow`](https://docs.oracle.com/javase/7/docs/api/javax/swing/table/DefaultTableModel.html#insertRow(int,%20java.util.Vector)) to replace the existing row – MadProgrammer Jan 07 '16 at 04:58

2 Answers2

1

As shown here, invoke setRowCount(0) to clear the table's model and then model.addRow(row) to add a new row.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

i use like this... and it's work for me

i use Netbeans IDE

public class test extends javax.swing.JFrame {

DefaultTableModel model ;

public test() {
    initComponents();
    model = (DefaultTableModel) table.getModel();
}

private void initComponents() {..}         

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    Object[] a = {"insert","test"};
    model.addRow(a);
}