Explore BrainMass

Explore BrainMass

    Java Programming ActionEvents

    Not what you're looking for? Search our solutions OR ask your own Custom question.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    I have a question about something that has puzzled me. I did this
    on my program because it was how I saw it done. I was wondering if
    someone could explain why it is done this way and if I would do this for
    all ActionEvents.

    I used the following code. What does the ".getSource()" do? Is that
    what registers when someone clicks the button? How would this format
    change if I was adding data entered into a text field? Any help is much
    appreciated!! :)

    ********

    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource() == b)
    {
    b.setText("Clicked");
    click++;
    if (click == 1)
    b.setText("I've been clicked!");
    else
    b.setText("I've been clicked " + click + " times!");
    }
    }

    ********

    © BrainMass Inc. brainmass.com March 6, 2023, 3:11 pm ad1c9bdddf
    https://brainmass.com/computer-science/java/java-programming-actionevents-128850

    Solution Preview

    e.getResource() method is to capture the resource which invokes actionPerformed method.
    We can consider the following situation to understand why e.getResource method is useful.
    Suppose I have 4 buttons, the actions of each button when pressed are ...

    Solution Summary

    The expert examines Java programming for ActionEvents.

    $2.49

    ADVERTISEMENT