File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Requesting help with question being asked. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Requesting help with question being asked." Watch "Requesting help with question being asked." New topic

Requesting help with question being asked.

kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54
I'm not sure how to answer this question and would appreciate some advice:

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?

Why is the decision statement mentioned in the previous question necessary?

Here is the method the question is referring to:

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

If it's not a JButton, then it would be some other Swing component which supports ActionListeners.

And if that were the case, then casting it to a JButton in line 4 of the posted code would throw a ClassCastException. That would be a bad thing; doing nothing would be better, and that's what line 1 does for you.
Darryl Burke

Joined: May 03, 2008
Posts: 5115

Where did you get that code, kenny? If it's provided as a sample of bad code that you have to improve, fine. But as an example of Swing and Java coding, it's atrocious.

For making an identity comparison using the == operator, casting is redundant. And since casting isn't needed, neither is the instanceof test.

And apart from all that, it suggests that a class having some other purpose of its own implements ActionListener, which is usually bad style.

luck, db
There are no new questions, but there may be new answers.
I agree. Here's the link:
subject: Requesting help with question being asked.
It's not a secret anymore!