简体   繁体   中英

Delete a row from a JList

I am making a project and for this project I need to delete a specific row from a Jlist. The Jlist is filled with data from a database.

Now when I click my Jbutton I can delete a row from the column code, but when I want to delete a new row I need to change the query in the new code.

ActionPerformed:

@Override
    public void Delete() {
        Connection conn;
        String sql = "DELETE FROM OEFENINGEN WHERE CODE = '5'"; 

        try{
            conn = OpenConnection();
            stmt = conn.createStatement();

            int result = stmt.executeUpdate(sql);
            if(result > 0){
                System.out.println("Record Delete");
            } else{
                System.out.println("Record NOT Delete");
            }
        }catch (SQLException e){
            e.printStackTrace();
        } finally {
            if (stmt != null){
                try{
                    stmt.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }  
            }
        }
        }   
    }

I want when you select a random row in the JList he delete the row from the database and Jlist when you click the Jbutton.

Make sure that each row of the list is an actual Object that represents the data in the database (or is at least carrying the required information to be identifiable)

Create yourself an Action , pass a reference of the JList `ListModel` to it.

When the actionPerformed method is fired, check which rows are selected, extract the row data from the ListModel and call you delete method. In the delete method, use the information from the rows to identify criteria for the delete statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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