简体   繁体   English

JTable标头突然更改了其列名

[英]JTable Header Suddenly Changed its Column Name

I have designed jtable like this: 我设计了这样的jtable:

在此处输入图片说明

And when I compiled my Java GUI Netbeans program, it has no problem: 当我编译Java GUI Netbeans程序时,它没有问题:

在此处输入图片说明

But after I press a JButton that I used this code to upload excel: 但是在按下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      
}                                        

suddenly the table columns name changed like this: 表格的列名称突然更改为:

在此处输入图片说明

So, what's wrong? 那怎么了 How do I fix this? 我该如何解决?

JTable Header Suddenly Changed its Column Name JTable标头突然更改了其列名

DefaultTableModel model2 = new DefaultTableModel(null, JdlTabel);

You specify "null" for the column headers when you recreate the table model. 重新创建表模型时,请为列标题指定“ null”。 so the default headings are used. 因此使用默认标题。

The solution is to either: 解决方案是:

  1. use the original column names when you recreate the model. 重新创建模型时,请使用原始列名。
  2. Don't recreate the entire model. 不要重新创建整个模型。 Instead you can remove all the rows from the current model using setRowCount(0) of your DefaultTableModel. Then you use the 相反,您可以使用DefaultTableModel. Then you use the setRowCount(0)从当前模型中删除所有行DefaultTableModel. Then you use the DefaultTableModel. Then you use the addRow(...) method of the DefaultTableModel` to load the data back into the model one row at a time. DefaultTableModel. Then you use the DefaultTableModel` DefaultTableModel. Then you use the addRow(...) method of the将数据一次加载回模型中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM