java 在netbeans中填充JTable中的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7333188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:33:42  来源:igfitidea点击:

Filling data in JTable in netbeans

javaswingnetbeansjtable

提问by Harshveer Singh

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

我想在运行时更改 JTable 中的默认数据。我正在使用 netbeans。我尝试了此处给出的解决方案,在使用 netbeans 时将数据添加到 JTable

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

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

but it gives me this error:enter image description here

但它给了我这个错误:在此处输入图片说明

回答by Captain Giraffe

If you want to change it at runtime you need to decide whenyou 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.

然后在适当的时候调用 setTableModel()。

回答by mKorbel

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

因为您在当前类之外编写了此代码行,因此您必须将这些代码行包​​装在 Class/void/constructor 中,例如

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);
    }
}