简体   繁体   English

如何设置仅在选择了jtable行后才能单击jbutton的条件

[英]How to make condition that jbutton can be clicked only if jtable row is selected

Hello guys I need your help, i am absolute beginner. 大家好,我需要您的帮助,我是绝对的初学者。 This is my problem, i have JTable in which i have some data from database with columns like: Id, Movie, Category, and Director. 这是我的问题,我有JTable,其中有一些来自数据库的数据,其中包括:Id,Movie,Category和Director。 With part 1 of code i am selecting that data in JTable and sending it to part 2 of code in order to show in other JFrame for purpose of editing data or deleting from Database. 在代码的第1部分中,我正在选择JTable中的数据并将其发送到代码的第2部分中,以便在其他JFrame中显示,以用于编辑数据或从数据库中删除。 I am using H2 database for my project. 我正在为我的项目使用H2数据库。 Now, my question is next, is there an option to create condition for that JButton to be clickable only if row is selected in JTable, or when is not selected row on JTable, and you press that JButton, to receive error message, and not to go to second JFrame. 现在,我的问题是下一个问题,是否有一个选项可以创建条件,使该JButton仅在JTable中选择了行,或者何时未在JTable上选择行并且您按该JButton来接收错误消息,才可以单击转到第二个JFrame。 The code is working right now, but if don't click anything on JTable fields in second JFrame are empty. 该代码现在可以正常工作,但是如果不单击第二个JFrame中的JTable字段,则该字段为空。

I hope that i was clear enough. 我希望我足够清楚。

part one 第一部分

       table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            DefaultTableModel model=(DefaultTableModel)table.getModel();
            int selectedRowIndex=table.getSelectedRow();
            Id=(model.getValueAt(selectedRowIndex, 0).toString());
            Movie=(model.getValueAt(selectedRowIndex, 1).toString());
            Category=(model.getValueAt(selectedRowIndex, 2).toString());
            Director=(model.getValueAt(selectedRowIndex, 3).toString());

           }
           });

part two 第二部分

    JButton btnDelete = new JButton("Delete");
    btnDelete.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            EditDelete rmv=new EditDelete();
            rmv.txtId.setText(Id);
            rmv.txtMovie.setText(Movie);
            rmv.txtCategory.setText(Category);
            rmv.txtDirector.setText(Director);
            rmv.setVisible(true);
            frmMovieDatabase.dispose();

            }
    });

Thanks in advance ! 提前致谢 !

The JTable API has a method to get the number of selected rows: getSelectedRowCount that you can use on the JButton ActionListener to check if you can perform the deletion. JTable API有一种获取选定行数的方法: getSelectedRowCount ,可以在JButton ActionListener上使用它来检查是否可以执行删除操作。

JButton btnDelete = new JButton("Delete");
    btnDelete.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (table.getSelectedRowCount() != 0){
               // perform the deletion
            }else{
               // Throw an exception etc...
            }
        }
    });

If you have any questions about handling or retrieving the selected values the page I linked above may have useful resources. 如果您对处理或检索选定的值有任何疑问,我上面链接的页面可能会有有用的资源。

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

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