目前,我正在尝试在java swing中向JTable添加行并获得以下输出。 Null附加显示java.lang.someerror
的第一个单元格This is the output with cells showing error
,我无法在表格中追加字符串。得到像
这样的错误无法将字符串转换为对象
需要插入表格(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"});
如果我使用此功能,请收到此错误...
请有人帮忙解决这个问题