简体   繁体   中英

Dynamically create JButton and add actionListener and in actionperformed function get record id

I am new to java and i want to delete records. I have dynamically n records with following code. I am getting records from following code.

ResultSet rs = stmt.executeQuery( "SELECT * FROM crud_records order by id desc" );      
    while ( rs.next() ) {
       int id               = rs.getInt("id");
       String idStr         = Integer.toString(id);
       String  username     = rs.getString("username");               
       String  email        = rs.getString("email");
       String  password     = rs.getString("password");
       String  dateAdded    = rs.getString("dateAdded");            

       listingFrame.add(new JLabel(idStr));
       listingFrame.add(new JLabel(username));
       listingFrame.add(new JLabel(email));
       listingFrame.add(new JLabel(password));
       listingFrame.add(new JLabel(dateAdded)); 

       delBtn.addActionListener(b2);

       listingFrame.add(new JButton("Delete"));               

    }

In this loop i want to create delete button for each records and i have added action listener to it. so what i want here

when user will click on delete button, i want to get specific record id/identification so that i can delete record.

I want to manage it dynamically means

  1. create delete button dynamically with unique identification
  2. dynamically get record id in actionPerformed function of actionListener interface and delete specific record.

Thanks in advance for your precious time.

That could look like :

   ResultSet rs = stmt.executeQuery( "SELECT * FROM crud_records order by id desc" );      
        while ( rs.next() ) {
           int id               = rs.getInt("id");
           String idStr         = Integer.toString(id);
           String  username     = rs.getString("username");               
           String  email        = rs.getString("email");
           String  password     = rs.getString("password");
           String  dateAdded    = rs.getString("dateAdded");                
           listingFrame.add(new JLabel(idStr));
           listingFrame.add(new JLabel(username));
           listingFrame.add(new JLabel(email));
           listingFrame.add(new JLabel(password));
           listingFrame.add(new JLabel(dateAdded)); 
           JButton specificDeleteBtn = new JButton("Delete "+idStr);
           specificDeleteBtn.setActinCommand(idStr);
           specificDeleteBtn.setActionListener(this);
           listingFrame.add(specificDeleteBtn);                  
        }
...
void actionPerformed(ActionEvent e) {
    String idStr = e.getActionCommand();
    // then you have the idStr of the item you want to remove...
}

The basic idea is to set some property in the button that will be available to the click handler. I used the basic action command construction, but you can also use an Action subclass (see setAction )

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