[英]How to add rows in JTable at runtime in Swing
目前,我正在尝试将行添加到 java swing 中的 JTable 并获得以下输出。 显示 java.lang.someerror 的第一个单元格附加 Null
,我无法在表中附加字符串。 收到类似的错误
无法将字符串转换为对象
需要插入代码中的表(String Object [] [])如下
class SimpleTableExample
extends JFrame
{
// Instance attributes used in this example
private JPanel topPanel;
private JTable table;
private JScrollPane scrollPane;
// Constructor of main frame
public SimpleTableExample()
{
// Set the frame characteristics
setTitle( "Simple Table Application" );
setSize( 1100, 150 );
setBackground( Color.gray );
// Create a panel to hold all other components
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create columns names
String columnNames[] = { " Date & Time", "VRP", "VYP", "VBP", "CRP","CYP","CBP","PO","BM","ARM","WT","RH","CNT","BCC","W","F","L","status"};
// Create some data
String dataValues[][] =
{
{ null, null, null , null, null, null, null, null , null, null,null, null, null , null, null, null, null, null },
{ null, null, null , null, null, null, null, null , null, null,null, null, null , null, null, null, null, null },
{ null, null, null , null, null, null, null, null , null, null,null, null, null , null, null, null, null, null },
{ null, null, null , null, null, null, null, null , null, null,null, null, null , null, null, null, null, null },
{ null, null, null , null, null, null, null, null , null, null,null, null, null , null, null, null, null, null },
{ null, null, null , null, null, null, null, null , null, null,null, null, null , null, null, null, null, null },
};
String Object[][] = {{"85", "85","85","85","85","85","85","85","85","85","85","85","85","85","85","85","85","85"}};
DefaultTableModel dm = new DefaultTableModel(0,0);
dm.setColumnIdentifiers(columnNames);
// Create a new table instance
table = new JTable(dm);
TableColumn column = null;
for(int i =0; i<18; i++){
column = table.getColumnModel().getColumn(i);
if(i==0)
{
column.setPreferredWidth(100);
}
else{
column.setPreferredWidth(40);
}
}
table.setModel(dm);
/* i have tried to use vector for insertion didn't work
Vector <Object> data = new Vector <Object>();
data.add(null);
data.add(null);
dm.addRow(data); */
((DefaultTableModel) ((JTable) table).getModel()).addRow(new Object[]{}); //for this code row got inserted with some error displaying in first cell
// Add the table to a scrolling pane
scrollPane = new JScrollPane(table);
topPanel.add( scrollPane, BorderLayout.CENTER );
}
// Main entry point for this example
public static void main( String args[] )
{
// Create an instance of the test application
SimpleTableExample mainFrame = new SimpleTableExample();
mainFrame.setVisible( true );
}
}
((DefaultTableModel) ((JTable) table).getModel()).addRow(new Object[]{"85","85","85","85","85","85","85","85","85","85","85","85","85","85","85","85","85","85"});
如果我使用它,则会出现此错误...
请任何人帮助解决这个问题
构造函数要求您提供一个使用附加大括号声明的 Object 数组。 我怀疑您使用的是String[]
而不是Object[][]
:
Object[][] defaultValues={
{"content1"},
{"content2"}
};
使用构造函数new DefaultTableModel(Object[][] data,Object[] columnames);
对我有用 .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.