我设计了像这样的jtable:
当我编译我的Java GUI Netbeans程序时,它没有问题:
但是在我按下JButton之后,我使用这段代码上传了excel:
private void b_browseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int column, row;
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(this);
File file = chooser.getSelectedFile();
chooser.setFileFilter(new FileNameExtensionFilter("Excel File", "xls"));
String al = file.getAbsolutePath();
File excelFile = new File(al);
if(!file.getName().endsWith("xls")){
JOptionPane.showMessageDialog(null,
"Pilih file Excel (.xls) saja!",
"Error",JOptionPane.ERROR_MESSAGE);
}
else
{
if (excelFile.exists()) {
try {
Workbook workbook = Workbook.getWorkbook(excelFile);
Sheet sheet = workbook.getSheets()[0];
TableModel model = new DefaultTableModel(sheet.getRows(), sheet.getColumns());
DefaultTableModel model2 = new DefaultTableModel(null, JdlTabel);
for (row = 0; row < sheet.getRows(); row++) {
for ( column = 0; column < sheet.getColumns(); column++) {
String content = sheet.getCell(column, row).getContents();
model.setValueAt(content, row, column);
data[row][column]=Double.parseDouble(content);
n_data = sheet.getRows();
k_data = sheet.getColumns();
}
}
j_latih.setModel(model2);
j_latih.setModel(model); //input data to Jtable
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error : " + e);
e.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "There is no data!"); }
}
cek_kmeans=0;
cek_data=1;//control tombol
}
突然表格列名改变如下:
那么,有什么不对?我该如何解决这个问题?
答案 0 :(得分:1)
JTable Header突然改变了其列名
DefaultTableModel model2 = new DefaultTableModel(null, JdlTabel);
重新创建表模型时,为列标题指定“null”。所以使用默认标题。
解决方案是:
setRowCount(0)
addRow(...)DefaultTableModel. Then you use the
DefaultTableModel`的method of the
删除当前模型中的所有行,以便一次一行地将数据加载回模型中