JTable新列添加不成功
JTable new column Add not successful
下面是我试图解析的部分代码。
我在这里尝试实现的是“在末尾再添加一个空白列,然后逐行填充该列的单元格值”。
尽管我可以调试,但在行 table.setValueAt(finalCell, row, table.getColumnCount()-1);
它正在尝试更新单元格 (1,5) 但在该行之后我的行变为 [你好,, , , 1] 来自 [Lee, Hsien杨, , , , 1]。
我期待的是[李显扬,1,你好]
但这里更新的是第一列而不是新的第 5 列。
我怎样才能实现我的目标?
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
Thanks but New column name is coming null
适合我...
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
时覆盖该列的数据
下面是我试图解析的部分代码。 我在这里尝试实现的是“在末尾再添加一个空白列,然后逐行填充该列的单元格值”。
尽管我可以调试,但在行 table.setValueAt(finalCell, row, table.getColumnCount()-1);
它正在尝试更新单元格 (1,5) 但在该行之后我的行变为 [你好,, , , 1] 来自 [Lee, Hsien杨, , , , 1]。
我期待的是[李显扬,1,你好]
但这里更新的是第一列而不是新的第 5 列。
我怎样才能实现我的目标?
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
Thanks but New column name is coming null
适合我...
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