Can ActionListener tell which button was pressed?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1411 Views - Last Post: 09 December 2011 - 03:01 PM Rate Topic: -----

#16 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5600
  • View blog
  • Posts: 9,037
  • Joined: 19-March 11

Re: Can ActionListener tell which button was pressed?

Posted 09 December 2011 - 12:43 PM

private JButton b1=null, b2=null, b3=null, b4=null, b5=null, b6=null, b7=null, b8=null, b9=null;


No need for this. Declaring and initializing an array of length n is equivalent to declaring n references to that type.

So these two lines:

JButton[] xx;
 xx = new JButton[9]; 


create the references you need for your buttons.

Now since you never initialize b1 through b9,

 xx[0] = b1;
...
 xx[8] = b9;




doesn't actually do anything. The array's contents were null before and they remain null after.

You can get rid of that part, in fact you can get rid of b1 through b9. The loop is what does the work, and it looks to me like you got that part right.

Small matter of style: I prefer to do my imports individually, rather than using the wildcard. It doesn't make a difference, since java only loads the ones it needs, but it serves as a good heads-up to someone reading your code, if they know ahead of time what libraries they're going to see. This is not very important, what you should take away from it is that everything in your code should be aimed at communicating your intent to the person reading it. Any code that you write after school will be either useful or not. If it's useful, it will be maintained, modified, and extended by people other than you. I presume you want to write useful code, therefore you should get in the habit of writing code that speaks to the maintainer.

This is actually much more important than comments - comments in the real world are only used sparingly, when something is not obvious in the code.
Was This Post Helpful? 1
  • +
  • -

#17 BMEdwards37  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 06-December 11

Re: Can ActionListener tell which button was pressed?

Posted 09 December 2011 - 01:17 PM

Setting each individual button to null was something i had tried earlier when i was getting a NullPointerException and then forgot to remove once i fixed it.

The reason i used the wildcard is mostly because i don't know the individuals so * is easier. Maybe learning the individual ones should be a new goal of mine.

As far as self-commenting code, does mine follow that (removing the comments obviously). Since you said comments are used sparingly, i would assume i have too many. Does my code effectively eliminate the use of comments or is there a way to make it more self-commenting?
Was This Post Helpful? 0
  • +
  • -

#18 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9153
  • View blog
  • Posts: 33,962
  • Joined: 27-December 08

Re: Can ActionListener tell which button was pressed?

Posted 09 December 2011 - 01:21 PM

You are overdoing it with these instance variables and the JButton[]. Just stick with the JButton[] of length 9, and use a loop to instantiate the new JButtons. No need for these instance fields.
private JButton b1=null, b2=null, b3=null, b4=null, b5=null, b6=null, b7=null, b8=null, b9=null;



@Sheph: Don't forget the semi-colon here JButton enter. :)

Edit: Whoops- majorly ninja'd by jon.kiparsky!
Was This Post Helpful? 2
  • +
  • -

#19 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5600
  • View blog
  • Posts: 9,037
  • Joined: 19-March 11

Re: Can ActionListener tell which button was pressed?

Posted 09 December 2011 - 01:30 PM

Quote

Since you said comments are used sparingly, i would assume i have too many.


No, for classroom code your comments are there for a different reason: in this case, they're there to tell the professor that you do in fact understand everything you're doing, and can explain it in your own words. When you're in industry, of course, we will assume that to be the case, and then you only want to comment things that are not obvious.

Quote

Maybe learning the individual ones should be a new goal of mine.


It's not at all a bad idea to learn the libraries well. You'll do that largely by using them, but reading through some of them and understanding what methods they offer will prime that pump.

For workhorse classes, you might want to look at String (of course), ArrayList, and the various wrapper classes to begin with (Integer, Double, Character, etc). I don't instantiate the wrappers very often, but they have useful utility methods in them. I hardly write a program without using ArrayLists somewhere, they're very useful. HashMap is another one to know. You probably don't need to memorize the APIs for these classes, but if you've read over their methods and maybe written little programs to try them out, I expect you'll find it easier to think of them when you need them.

In Swing, probably getting your head around JFrame, JPanel, JButton, and JTextField/JTextArea will probably give you a good leg up.
Was This Post Helpful? 1
  • +
  • -

#20 BMEdwards37  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 06-December 11

Re: Can ActionListener tell which button was pressed?

Posted 09 December 2011 - 03:01 PM

Now i understand what you meant by the buttons not being needed along with the array. Mac, Jon may have ninja'd you but i didn't catch that part until i read your post :bigsmile:

As far as the rest of GUI programming, this part was only half of the lab, the other part had to do with JFrame, JPanel, layouts, etc. so i am getting a grasp of that too.

Jon thanks for all your help with this particular topic. Mac, i was looking around the site earlier and saw the incredible amount of resources/tutorials/etc. that you have posted and would like to give you a special thank you for all of that, i haven't begun to look at all of it yet but i can tell there is a wealth of information there.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2