1

I want to change default data in JTable at runtime. I am using netbeans. I tried solution given here adding data to JTable when working with netbeans

jTable1.getModel().setValueAt(row, column, value);

but it gives me this error:enter image description here

Community
  • 1
  • 1
Harshveer Singh
  • 4,037
  • 7
  • 37
  • 45

2 Answers2

4

If you want to change it at runtime you need to decide when you want it changed and add the code to the proper method/event. As it stands you have a method call in your class definition, which is not valid code.

For instance

public void setTableModel(){
    displayTable.getModel().setValueAt(2,4,300);
}

And then call setTableModel() at an appropriate time.

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
1

because you wrote this code line out of current Class, you have to wrap these code line(s) inside Class/void/constructor, for example

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TableProcessing extends JFrame implements TableModelListener {

    private static final long serialVersionUID = 1L;
    private JTable table;

    public TableProcessing() {
        String[] columnNames = {"Item", "Quantity", "Price", "Cost"};
        Object[][] data = {
            {"Bread", new Integer(1), new Double(1.11), new Double(1.11)},
            {"Milk", new Integer(1), new Double(2.22), new Double(2.22)},
            {"Tea", new Integer(1), new Double(3.33), new Double(3.33)},
            {"Cofee", new Integer(1), new Double(4.44), new Double(4.44)}
        };
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        model.addTableModelListener(this);
        table = new JTable(model) {

            private static final long serialVersionUID = 1L;

            @Override
            public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }

            @Override
            public boolean isCellEditable(int row, int column) {
                int modelColumn = convertColumnIndexToModel(column);
                return (modelColumn == 3) ? false : true;
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);
    }

    @Override
    public void tableChanged(TableModelEvent e) {
        if (e.getType() == TableModelEvent.UPDATE) {
            int row = e.getFirstRow();
            int column = e.getColumn();
            System.out.println(row + " : " + column);
            if (column == 1 || column == 2) {
                int quantity = ((Integer) table.getModel().getValueAt(row, 1)).intValue();
                double price = ((Double) table.getModel().getValueAt(row, 2)).doubleValue();
                Double value = new Double(quantity * price);
                table.getModel().setValueAt(value, row, 3);
            }
        }
    }

    public static void main(String[] args) {
        TableProcessing frame = new TableProcessing();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
} 
mKorbel
  • 109,525
  • 20
  • 134
  • 319