swing - Getting source of an event in Java -


Is there a way to get the source of an event? I know event.getSource () However, is it any way to convert a string?

For example, if the source is a button, then the button 1 is any where the value for the string is to assign the button 1 variable? (I am working with so many buttons and therefore, if I can not write a statement)

For the sake of clarity:

The law returns the object from which the incident originated. You can use it to get some kind of property from the element, such as the name of the text or button inside the label.

These are the strings, but if you decide to go this route, then I make sure that you select some components that are similar to all the components which ActionListerner .

This is the place where you can work, you can set a unique 'identifier' when the components are made, and can access them later.

  Jebton Button = New Pocket ("Button"); Button.setActionCommand ("1"); Pocket button = new pocket ("button"); Button.setActionCommand ("2");  

Then you can compare these options to any method, or you can do some fancy, in this way (because you said that you use the other statements Do not want to):

  string command = e.getActionCommand (); Int i = integer. Parsient (command); Switch (i) {Case 1: // Break something; }  

According to the Java docs:

This string gives the string command associated with this verb. This string is a "model" component with many commands based on its state Allows to specify one of the. For example, toggle between a button "Show details" and "hide details" could be the source object and the event will be identical in each case, but the command string will identify the desired action.

Keep in mind that I think this is the only best way if you are using a ActionListerner for many components as mentioned in another answer. You can create unique ActionListeners on each button.

Hope this helps you!


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -