以下是我要解决的代码的一部分。 我在这里尝试实现的是" 在末尾添加一个空白列,然后逐行填充该列的单元格值"。
尽管我可以调试,但是在行 table.setValueAt(finalCell,row,table.getColumnCount() - 1);
它试图更新单元格(1,5)但在该行之后我的行变为 [Hello ,,,, 1] 来自 [Lee,Hsien Yang ,,, 1] 即可。
我期待的是 [Lee,Hsien Yang ,,,, 1,Hello]
但是这里更新了第一列,而不是新的第五列。
我如何实现目标?
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);
}
答案 0 :(得分: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);
}
});
}
}
看起来很奇怪的是,你是示例代码;
[Lee,Hsien Yang ,,, 1]
有6列,但你只允许5 ...
String[] columnNames = {"People", "Place", "Organisation", "Event", "Mentions"};
当您致电addColumn