The moose likes Swing / AWT / SWT and the fly likes Could someone tell me if I answered this question correctly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Could someone tell me if I answered this question correctly?" Watch "Could someone tell me if I answered this question correctly?" New topic

Could someone tell me if I answered this question correctly?

kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54

I'm being asked this question:

In the actionPerformed(ActionEvent event) method in the class InputPanel, the first line of code is a decision statement determining if the event.getSource is a JButton. If the source is NOT a JButton, what else would it be?

Here is the class the question is referring to:

I believe it will be setSales. Is this correct? Thank you for any help given!
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38048
When I got as far as line 7, the answer was no. Even without reading the rest of it.
The source could in theory be anything. I am pretty sure that text components (including text fields) can fire action events if you push the enter key when they have focus, so that is one possibility. The instanceof operator is used to test whether the following cast is safe. So you now have three non‑object‑oriented features for the price of one:-
  • Making a GUI component a listener, unless it is listening for events originating in itself.
  • Casting
  • Use of instanceof.
  • You only have one source for the events, so add the listener to that source. Create a class which handles that event. Not the panel class.
    Lots more in that code:-
  • You have almost certainly got too many fields in that class. A lot of those fields should be local variables.
  • Why are you returning an array of text fields from your method?
  • Why are you using int[]s rather than creating a Salesman class and having Salespeople arrays?
  • Why are you using "north", etc., for the Border Layout rather than the constants defined in the Border Layout class?

  • I shall move this discussion to our GUI forum because there is so much GUI‑related stuff to the question.
    Paul Clapham

    Joined: Oct 14, 2005
    Posts: 18541

    This code:

    is the only place where the InputPanel object is added as an ActionListener to any component. So the question is just as bad as the code is.

    Why are you being asked this question?
    Don't get me started about those stupid light bulbs.
    subject: Could someone tell me if I answered this question correctly?
    Similar Threads
    Browse and Load a Text File
    completely lost in terms of trying to understand Collections.synchronizedList
    Event listener not responding
    GridBagLayout problem
    Please help with a simple GUI.