Java 如何在JTable中设置特定单元格的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21158083/
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-08-13 07:00:11  来源:igfitidea点击:

How to set the value of specific cell in JTable?

javaswingjtable

提问by gihooh

I just want to know how to update specific cell in JTable,

我只是想知道如何update specific cell in JTable

like if I want to set the cell(1,1) to have the value of Test Value.

就像我想将单元格(1,1)设置为Test Value.

my code goes like this but does not work for me:

我的代码是这样的,但对我不起作用:

String s = "Test Value";
tableName.setValueAt((Object)s, 1, 1);

采纳答案by Paul Samsotha

You may be looking to use the DefaultTableModel#setValueAt()

您可能正在寻找使用 DefaultTableModel#setValueAt()

DefaultTableModel model = (DefaultTableModel)tableName.getModel();

model.setValueAt(s, 1, 1);

You first need to specify the table with the DefaultTableModel

您首先需要使用 DefaultTableModel

 DefaultTableModel model = new DefaultTableModel(data, cols);
 JTable table = new JTable(model);


You can run this example to see

你可以运行这个例子来看看

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class TestTable {
    String[] cols = {"Col 1", "Col2"};
    String[][] data = {{"Hello", "World"},{"Hello", "World"}};
    DefaultTableModel model = new DefaultTableModel(data, cols);
    JTable table = new JTable(model);
    JButton button = new JButton("Set Value at 1, 1");
    JTextField text = new JTextField(20);

    public TestTable() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(table, BorderLayout.NORTH);
        panel.add(text, BorderLayout.CENTER);
        panel.add(button, BorderLayout.SOUTH);

        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                String value = text.getText();
                model.setValueAt(value, 1, 1);
            }
        });

        JFrame frame = new JFrame();
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
               new TestTable();
            }
        });
    }
}

enter image description here

在此处输入图片说明



EDITUsing Product class.

编辑使用产品类。

"I created a class Products with 8 fields, What I want to do is populate the data in ArrayList to my table using loop"

“我创建了一个包含 8 个字段的产品类,我想要做的是使用循环将 ArrayList 中的数据填充到我的表中”

You want to add rows dynamically by using .addRow. You need to get each field from each Productand make it a row. Then add that row like this. Note: You should use gettersbut for brevity, I didn't.

您想通过使用动态添加行.addRow。您需要从每个字段中获取每个字段Product并将其排成一行。然后像这样添加该行。注意:您应该使用,getters但为简洁起见,我没有使用。

for (Product p : list) {
    String data1 = p.field1;
    int data2 = p.field2;
    int data3 = p.field3;
    int data4 = p.field4;
    int data5 = p.field5;
    int data6 = p.field6;
    int data7 = p.field7;
    int data8 = p.field8;

    Object[] row = {data1, data2, data3, data4, data5, data6, data7, data8};
    model.addRow(row);

}


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class TestTable {
    String[] cols = {"Col 1", "Col 2", "Col 3", "Col 4", "Col 5", "Col 6", "COl 7", "Col 8"};
    DefaultTableModel model = new DefaultTableModel(cols, 0);
    JTable table = new JTable(model);
    JButton button = new JButton("Set Table");
    List<Product> list;

    public TestTable() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(new JScrollPane(table), BorderLayout.CENTER);
        panel.add(button, BorderLayout.SOUTH);

        list = getOneRow();

        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                for (Product p : list) {
                    String data1 = p.field1;
                    int data2 = p.field2;
                    int data3 = p.field3;
                    int data4 = p.field4;
                    int data5 = p.field5;
                    int data6 = p.field6;
                    int data7 = p.field7;
                    int data8 = p.field8;

                    Object[] row = {data1, data2, data3, data4, data5, data6, data7, data8};
                    model.addRow(row);

                }
            }
        });

        JFrame frame = new JFrame();
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public List<Product> getOneRow() {
        List<Product> list = new ArrayList<>();
        list.add(new Product("Product 1", 1, 2, 3, 4, 5, 6, 7));
        list.add(new Product("Product 2", 1, 2, 3, 4, 5, 6, 7));
        list.add(new Product("Product 3", 1, 2, 3, 4, 5, 6, 7));
        list.add(new Product("Product 4", 1, 2, 3, 4, 5, 6, 7));


        return list;
    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
               new TestTable();
            }
        });
    }
}

class Product {
    String field1;
    int field2;
    int field3;
    int field4;
    int field5;
    int field6;
    int field7;
    int field8;

    public Product(String s, int f2, int f3, int f4, int f5, int f6, int f7, int f8) {
        field1 = s;
        field2 = f2;
        field3 = f3;
        field4 = f4;
        field5 = f5;
        field6 = f6;
        field7 = f7;
        field8 = f8;

    }  
}