[英]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.