Java 动态创建JButton并添加actionListener,并在actionperformed函数中获取记录id
我是java新手,我想删除记录。我有以下代码的动态n个记录。我从以下代码中获取记录Java 动态创建JButton并添加actionListener,并在actionperformed函数中获取记录id,java,android,Java,Android,我是java新手,我想删除记录。我有以下代码的动态n个记录。我从以下代码中获取记录 ResultSet rs = stmt.executeQuery( "SELECT * FROM crud_records order by id desc" ); while ( rs.next() ) { int id = rs.getInt("id"); String idStr = Integer.toString
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"));
}
在这个循环中,我想为每个记录创建一个删除按钮,并向其中添加了操作侦听器。那我想要什么呢
当用户点击删除按钮时,我想获得具体的记录id/标识,以便删除记录
我想动态管理它意味着
使用唯一标识动态创建删除按钮
在actionListener接口的actionPerformed函数中动态获取记录id并删除特定记录。
提前感谢您宝贵的时间。这可能看起来像:
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...
}
基本思想是在按钮中设置一些属性,这些属性将可供单击处理程序使用。我使用了基本的action命令构造,但也可以使用action子类see setAction