以下是我要解决的部分代码。
我要在这里实现的目标是“在末尾再添加一个空白列,然后为该列逐行填充单元格值”。

即使我可以在table.setValueAt(finalCell,row,table.getColumnCount()-1)行中进行调试,

它试图更新单元格(1,5),但是在那行之后,我的行从[Lee,Hsien Yang,,,,1]变成[Hello,,,,1]。

我期望的是[Lee,Hsien Yang,,,,1,您好]

但是这里更新的第一列而不是新的第五列。

我如何实现我的目标?

            CSVReader reader = new CSVReader(new FileReader(file));

            List<String[]> myEntries = reader.readAll();
            String[][] rowData = myEntries.toArray(new String[0][]);

            String[] columnNames = { "People", "Place", "Organisation", "Event", "Mentions" };

            JTable table=new JTable(rowData, columnNames);

            TableColumn newColumn=new TableColumn();
            table.getColumnModel().addColumn(newColumn);

            Boolean isFirstLine=true;

            for(int row=0;row< table.getRowCount();row++)
            {
                if(isFirstLine)
                {
                    isFirstLine=false;
                    continue;
                }
                String finalCell="";

                for(int col=0;col<table.getColumnCount()-1;col++)
                {
                    finalCell="Hello";
                }
                table.setValueAt(finalCell, row, table.getColumnCount()-1);
            }

最佳答案

使用适当的TableModel

            CSVReader reader = new CSVReader(new FileReader(file));

            List<String[]> myEntries = reader.readAll();
            String[][] rowData = myEntries.toArray(new String[0][]);

            String[] columnNames = {"People", "Place", "Organisation", "Event", "Mentions"};

            DefaultTableModel tableModel = new DefaultTableModel(rowData, columnNames);

            tableModel.addColumn(""); // What ever name you want to give it...

            Boolean isFirstLine = true;

            for (int row = 0; row < tableModel.getRowCount(); row++) {
                if (isFirstLine) {
                    isFirstLine = false;
                    continue;
                }
                String finalCell = "";

                for (int col = 0; col < tableModel.getColumnCount() - 1; col++) {
                    finalCell = "Hello";
                }
                tableModel.setValueAt(finalCell, row, tableModel.getColumnCount() - 1);
            }

            JTable table = new JTable(tableModel);


TableModel通常负责管理数据,通常是放置这些内容的地方。

有关更多详细信息,请参见How to Use Tables


谢谢,但是新列名称为空


对我来说很好...



import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                String[] columnNames = {"People", "Place", "Organisation", "Event", "Mentions"};

                String[][] rowData = new String[][]{
                    {"Lee", "Hsien Yang", "", "", ""}
                };

                DefaultTableModel tableModel = new DefaultTableModel(rowData, columnNames);

                tableModel.addColumn(""); // What ever name you want to give it...

                Boolean isFirstLine = true;

                for (int row = 0; row < tableModel.getRowCount(); row++) {
                    tableModel.setValueAt("Hello", row, tableModel.getColumnCount() - 1);
                }

                JTable table = new JTable(tableModel);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}


看起来很奇怪的是,您是示例代码;


[李显贤,,,,1]


有6列,但您只允许5列...

String[] columnNames = {"People", "Place", "Organisation", "Event", "Mentions"};


当您调用addColumn时,这将覆盖该列的数据

07-24 13:35