How to call self component in Java swing?
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"
}
精彩评论