简体   繁体   中英

Java Swing Buttons

So I have three buttons: add, edit and save. I am using ActionListener and getActionCommand to determine which button was pressed. If I press add, then save, it has to add a new person in my table. If I press edit, then save, it has to edit that person in the table. Is there any way to determine which button was pressed before save so that I know which way I go in Save button?

Yes. Store which button was pressed in an instance variable in your class (I don't mean in your listener class).

by using getSource() we can do it

and add some conditions like flag=1 in add button, flag=2 in edit button

if flag=1 then add new record to a table

if flag=2 then edit existing record.

A better way than using ActionListeners directly is to use Actions:

http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html

I always felt like getSource was a big hack, don't know why you would ever need to use it.

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