简体   繁体   English

动态创建JButton并添加actionListener并在actionperformed函数中获取记录ID

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

I am new to java and i want to delete records. 我是Java新手,我想删除记录。 I have dynamically n records with following code. 我有以下代码动态地记录n。 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. 当用户单击删除按钮时,我想获取特定的记录ID /标识,以便我可以删除记录。

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. 在actionListener接口的actionPerformed函数中动态获取记录ID并删除特定记录。

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 ) 我使用了基本的action命令构造,但是您也可以使用Action子类(请参见setAction

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

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