簡體   English   中英

當按下JButton時,JTable第二次變為空白

[英]JTable goes blank for second time when JButton is pressed

我正在嘗試在JTable中加載文件名以及一些詳細信息,第一次按下按鈕時,它可以正常加載 ,但是第二次按下同一按鈕時,它變為空白

簡短示例:

package exampleDemo;

public class JTableExample {

private JFrame frame;
private JTable table_1;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                JTableExample window = new JTableExample();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public JTableExample() {
    initialize();
}

    public void showEmptyTable() {
        final String[] colNames = {"Sl.no.", "File Name", "Size", "Last Modified", "PO", "LN", "SI", "MS", "PT"};
        final String[][] emptyData = {
            {"", "", "", "", "", "", "", "", "",},
            {"", "", "", "", "", "", "", "", "", },
            {"", "", "", "", "", "", "", "", "", },
            {"", "", "", "", "", "", "", "", "", },
            {"", "", "", "", "", "", "", "", "", },};
    table_1.setModel(new DefaultTableModel(emptyData, colNames));
    frame.getContentPane().add(table_1);
    JScrollPane scrollPane = new JScrollPane(table_1);
    scrollPane.setBounds(81, 162, 830, 180);
    frame.getContentPane().add(scrollPane);
};

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 1002, 576);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    table_1 = new JTable();
    table_1.setBounds(81, 162, 830, 180);
    frame.getContentPane().add(table_1);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            table_1.repaint(); 
            showEmptyTable();
        }
    });
    btnNewButton.setBounds(578, 70, 89, 23);
    frame.getContentPane().add(btnNewButton);
    }
}

快速瀏覽一下您的代碼將向我建議您使用這種錯誤的方式:每次單擊按鈕時,您實際上都是在重建一個新的表模型,但我懷疑(盡管目前只是一種預感)因為每次單擊該按鈕時,都是通過新的滾動面板將相同的表添加到主JFrame中-每次。

您要做的是在加載框架期間初始化表格及其模型。 然后,如果要清除表,則只需獲取對該表的模型的引用並操作現有模型。 就是這樣-無需創建全新模型。 無需通過滾動面板添加新表。

因此,在您的情況下,我要做的是在構造函數中添加一行:

public YourClassName() {
  initialize();
  initializeYourTable();
}

然后initializeYourTable()將如下所示:

private void initSearchTable() {
    final String[] colNames = {"Sl.no.", "File Name", "Size", "Last Modified", "PO", "LN", "SI", "MS", "PT"};
    //PO: Part Order
    //LN: Line Number
    //SI: Serial Number
    //MS: Material Supplier
    //PT: Part Item 
    final String[][] emptyData = {
        {"", "", "", "", "", "", "", "", "",},
        {"", "", "", "", "", "", "", "", "",},
        {"", "", "", "", "", "", "", "", "",},
        {"", "", "", "", "", "", "", "", "",},
        {"", "", "", "", "", "", "", "", "",},};
    table_3.setModel(new DefaultTableModel(emptyData, colNames));
    //frmViperManufacturingRecord.getContentPane().add(table_3); <-- NB this should be commented out!!
    table_3.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table_3.getColumnModel().getColumn(0).setPreferredWidth(40);
    table_3.getColumnModel().getColumn(1).setPreferredWidth(290);
    table_3.getColumnModel().getColumn(2).setPreferredWidth(80);
    table_3.getColumnModel().getColumn(3).setPreferredWidth(178);
    table_3.getColumnModel().getColumn(4).setPreferredWidth(25);
    table_3.getColumnModel().getColumn(5).setPreferredWidth(25);
    table_3.getColumnModel().getColumn(6).setPreferredWidth(25);
    table_3.getColumnModel().getColumn(7).setPreferredWidth(25);
    table_3.getColumnModel().getColumn(8).setPreferredWidth(25);
    JScrollPane scrollPane = new JScrollPane(table_3);
    scrollPane.setBounds(324, 209, 713, 160);
    frmViperManufacturingRecord.getContentPane().add(scrollPane);
}

要清除表格,您可以使用以下快捷方式

private void resetTable() {
    DefaultTableModel model = (DefaultTableModel)table_3.getModel();
    model.setRowCount(0);
}

因此,您需要更改actionPerformed

public void actionPerformed(ActionEvent arg0) {
  table_3.repaint();
  //showEmptyTable();
  resetTable();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM