ListSelectionListener只需单击一次即可进行两次事件

时间:2018-09-12 07:39:48

标签: event-handling jtable mouseevent mouseclick-event

我正在编写一个程序,可以在其中搜索数据库中的项目。 为了跟踪该程序的使用时间,我添加了一个计数。因此,每当有人单击按钮或特定行时,计数器就会变为+1,我可以看到该程序在我们公司中使用了多少次。

对于每一个按钮,每次单击JTable中的一个单元格时,它的工作方式都应该达到预期的效果,除了不是按钮而是ListSelectionListener的按钮。它执行两次操作,计数增加2,而不是1。

我似乎找不到为什么它执行两次的原因(我添加了一些打印命令来检查代码在哪里重复,但是他重复了全部)

        table = new JTable();
    table.setFillsViewportHeight(true);
    table.setColumnSelectionAllowed(true);
    table.setCellSelectionEnabled(true);
    table.getSelectionModel().addListSelectionListener(new ListSelectionListener () {
        public void valueChanged(ListSelectionEvent event) {
            if(table.getSelectedRow() > -1){
                String temp =table.getValueAt(table.getSelectedRow(),table.getSelectedColumn()).toString();
                LBL_Test.setText(temp);
                connect.setTable_search_count1(gebruiker_keuze.getGebruiker());
                System.out.println("a");
                if(temp.contains("Ifc")){
                    System.out.println("b");
                    connect.Connect_Double_Table(temp, table_1, table_2,
                            gebruiker_keuze.getGebruiker(),
                            connect.getTable_search_count1(),
                            "Table_search_count1_string");
                    System.out.println("Match gevonden : tabel ingevoerd");
                }else{
                    System.out.println("Geen match gevonden ");
                }               
            }
        }
    });
    scrollPane.setViewportView(table);

在代码中,我使用connect来引用连接到数据库的类。

该类看起来像这样:

public void Connect_Double_Table(String query_invoer, JTable table_invoer,JTable table_invoer2,
        String gebruiker_invoer, int count_invoer, String BTN_count_string) {
    connection();
    String temp = null;
    try{
        con = DriverManager.getConnection(host,Username,Password);
        String q1 = "SELECT TypeEntitie FROM typeentities WHERE Typeentitie = '" + query_invoer + "';";
        ps = con.prepareStatement(q1);
        rs = ps.executeQuery();
        table_invoer.setModel(DbUtils.resultSetToTableModel(rs));

        String q2 = "SELECT id FROM typeentities WHERE typeentitie = '" + query_invoer + "';";
        ps1 = con.prepareStatement(q2);
        rs1 = ps1.executeQuery();
        while(rs1.next()){
            temp = rs1.getString(1);
        }
        String q3 = "SELECT TypeEnumerations_sub FROM typeenumerations where id_TypeEntities = '" + temp + "';" ;
        ps2 = con.prepareStatement(q3);
        rs2 = ps2.executeQuery();

        table_invoer2.setModel(DbUtils.resultSetToTableModel(rs2));
        if(BTN_count_string == "pulldown_1"){
            count_invoer +=1;
            String q = "UPDATE gebruikers_HFB SET pulldown_1 = ? WHERE gebruiker = ?;";
            ps3 = con.prepareStatement(q);
            ps3.setInt(1, count_invoer);
            ps3.setString(2, gebruiker_invoer);
            ps3.executeUpdate();
            Pulldown_Count1 = count_invoer;
            con.close();
        }if(BTN_count_string == "Table_search_count1_string"){
            count_invoer +=1;
            String q = "UPDATE gebruikers_HFB SET Table_search_count1 = ? WHERE gebruiker = ?;";
            ps4 = con.prepareStatement(q);
            ps4.setInt(1, count_invoer);
            ps4.setString(2, gebruiker_invoer);
            ps4.executeUpdate();
            Table_search_count1 = count_invoer;
            con.close();
        }if(BTN_count_string == "Table_search_count2_string"){
            count_invoer +=1;
            String q = "UPDATE gebruikers_HFB SET Table_search_count2 = ? WHERE gebruiker = ?;";
            ps4 = con.prepareStatement(q);
            ps4.setInt(1, count_invoer);
            ps4.setString(2, gebruiker_invoer);
            ps4.executeUpdate();
            Table_search_count2 = count_invoer;
            con.close();
        }
    }catch(Exception e){
        JOptionPane.showMessageDialog(null,e);
    }
}

奇怪的是,我在同一程序的另一个表上使用了ListSelectionListener,每当我单击那里时,它只会执行一次。

那部分看起来像这样:

table_1 = new JTable();
    table_1.setFillsViewportHeight(true);
    table_1.getSelectionModel().addListSelectionListener(new ListSelectionListener () {
        public void valueChanged(ListSelectionEvent event) {
            if(table_1.getSelectedRow() > -1){
                String temp =table_1.getValueAt(table_1.getSelectedRow(),table_1.getSelectedColumn()).toString();
                connect.setPulldown_count1(gebruiker_keuze.getGebruiker());
                connect.Connect_Double_Table(temp, table_1, table_2,
                                            gebruiker_keuze.getGebruiker(),
                                            connect.getTable_search_count2(),
                                            "Table_search_count2_string");
            }
        }
    });

我真的迷失了这个,有人可以帮忙吗?

0 个答案:

没有答案