3

I am new to JTable.

I want to update the jtable data at runtime in button press event.

Here is my code.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.*;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class SetEditableTableCell extends JPanel {
JPanel top = new JPanel();
JPanel bottom = new JPanel();
JButton update = new JButton("Update");
JTable table;
Vector<String> rowOne;
Vector<String> rowTwo;
DefaultTableModel tablemodel;
public SetEditableTableCell() {
    this.setLayout(new BorderLayout());
    rowOne = new Vector<String>();
    rowOne.addElement("Row1-1");
    rowOne.addElement("Row1-2");
    rowOne.addElement("Row1-3");
    rowTwo = new Vector<String>();
    rowTwo.addElement("Row2-2");
    rowTwo.addElement("Row2-3");
    rowTwo.addElement("Row2-4");
    Vector<Vector> rowData = new Vector<Vector>();
    rowData.addElement(rowOne);
    rowData.addElement(rowTwo);
    Vector<String> columnNames = new Vector<String>();
    columnNames.addElement("Column One");
    columnNames.addElement("Column Two");
    columnNames.addElement("Column Three");
    tablemodel = new DefaultTableModel(rowData, columnNames);
    table = new JTable(tablemodel);
    //  table.setValueAt("aa", 0, 0);    
    update.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            updatedata();
        }
        private void updatedata() {
            rowOne = new Vector<String>();
            rowOne.addElement("updated Row1-1");
            rowOne.addElement("updated Row1-2");
            rowOne.addElement("updated Row1-3");
            rowTwo = new Vector<String>();
            rowTwo.addElement("updated Row2-2");
            rowTwo.addElement("updated Row2-3");
            rowTwo.addElement("updated Row2-4");
            // tablemodel.addRow(rowTwo);
            tablemodel.fireTableDataChanged();
            table.setModel(tablemodel);
            System.out.println("button pressed");
            // table.setValueAt("aa", 0, 0);    
        }
    });
    JScrollPane scrollPane = new JScrollPane(table);
    top.add(scrollPane);
    bottom.add(update);
    add(top, BorderLayout.NORTH);
    add(bottom, BorderLayout.SOUTH);
}
public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    SetEditableTableCell obj = new SetEditableTableCell();
    frame.add(obj);
    //frame.setSize(400, 300);
    frame.pack();
    frame.setVisible(true);
}
}

But it is not updating after pressing update button.

Can anybody solve my problem?

Thanks in advance..

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Babu R
  • 1,025
  • 8
  • 20
  • 40

2 Answers2

3

It is not updated because you do not modify the values in your TableModel. By assigning a new Vector to rowOne and rowTwo in your updateData method, you are altering other Vector instances then the ones your TableModel knows about.

A possible solution is to reconstruct the data vector and use the setDataVector method

Robin
  • 36,233
  • 5
  • 47
  • 99
  • Yes it's get updated. It will append with previous data. But i want to replace old values with the new one. How is it possible? – Babu R Jul 21 '12 at 07:36
0

Construct your own table model using the AbstractTableModel and use it's "event" triggers to notify the JTable of changes to the underlying model

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366