简体   繁体   English

没有选择jTable行时如何显示消息?

[英]How to show message when no jTable row is selected?

I have a jButton and when it is clicked currently I can select multiple rows and I am able to delete all of them. 我有一个jButton,当当前单击它时,我可以选择多行,并且可以删除所有行。 I am trying to make a message pop up if no row is selected to warn the user. 如果没有选择任何行来警告用户,我试图使消息弹出。 I tried different things, but I couldn't manage it in the end unfortunately. 我尝试了不同的方法,但不幸的是最终我无法解决。

private void silButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
    try {
        int[] selectedItems = jTable2.getSelectedRows();
        for (int i = 0; i < selectedItems.length; i++) {
            String cell = (jTable2.getModel().getValueAt(selectedItems[i], 0)).toString();
            System.out.println(cell);
            String query="DELETE FROM Musteriler WHERE id="+cell+"";
            PreparedStatement pst = connection.prepareStatement(query);
            int rs = pst.executeUpdate();
        }

        loadTable();
        cleanFields();
    } catch (Exception e) {
        System.out.println(e);
    }
}           

Simply just test that selectedItems length is zero or not: 只需测试一下selectedItems长度是否为零即可:

if(selectedItems == null || selectedItems.length() < 1) {
    showPopup();
}

API reference: API参考:

public int[] getSelectedColumns​()

Returns: an array of integers containing the indices of all selected columns, or an empty array if no column is selected. 返回:包含所有选定列的索引的整数数组;如果未选择任何列,则为数组。

Basically you should check if the selectedItems is empty and show your modal dialog. 基本上,您应该检查selectedItems是否为空,并显示模式对话框。

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

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