开发者

How to call self component in Java swing?

开发者 https://www.devze.com 2023-03-10 05:48 出处:网络
I have many buttons in a button group that need to search a database using their containing text as the query when toggled.

I have many buttons in a button group that need to search a database using their containing text as the query when toggled.

Instead of typing out specific event code for each button, how do I call a button's self?

Desired pseudocode: 开发者_JAVA百科 searchDB(genericSelf.getText())

Tried using the this keyword and fiddling with getComponent, but I'm sure there exists a more efficient way.


How about:

public void actionPerformed(ActionEvent evt) {
    JButton source = (JButton) evt.getSource();
    // source is your "this"
}
0

精彩评论

暂无评论...
验证码 换一张
取 消