Results 1 to 6 of 6
  1. #1
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Referencing All Parts of an Array

    How can I reference the entire set of array values (or in my case objects) with one piece of code?

    For example, I have a grid of JButtons and I use the same if-statement within a for-loop for every button (in the array). I want to use "(e.getSource() == buttons[?????])". Here is my code.
    Java Code:
    if (e.getSource() == buttons[?????]) {
                for (int i=0; i<buttons.length; i++) {
                    if (e.getSource() == buttons[i]) {
                        if (counts[i] == 0) {
                            buttons[i].setBackground(Color.RED);
                            counts[i] = 1;
                        } else if (counts[i] == 1) {
                            buttons[i].setBackground(Color.BLUE);
                            counts[i] = 0;
                        }
    How should I do that?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Referencing All Parts of an Array

    Your for loop makes sense, but the first if block doesn't. It would help if you explained just what you're trying to achieve with that bit of code.

  3. #3
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Referencing All Parts of an Array

    Basically, if the source of the click is any of the buttons in the array "buttons", then do this.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Referencing All Parts of an Array

    Best to get rid of that if and to just to iterate through the array and see if the source is any of the buttons. But then again, if you only add this ActionListener to the button array and to nothing else, then you'll already know that the source is one of the buttons.

  5. #5
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Referencing All Parts of an Array

    It has a few separate buttons also. So I should make a separate for loop for my array? OK. Thank you for your quick response :D

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Referencing All Parts of an Array

    Quote Originally Posted by Prime624 View Post
    It has a few separate buttons also. So I should make a separate for loop for my array? OK. Thank you for your quick response :D
    The separate buttons should likely have their own ActionListeners. Don't force an ActionListener to have too much responsibility.

Similar Threads

  1. Help with referencing two objects
    By Bluzman in forum New To Java
    Replies: 3
    Last Post: 01-30-2012, 12:32 AM
  2. Replies: 0
    Last Post: 09-12-2011, 01:06 PM
  3. Print Parts Of An Array [User Input]
    By PrimalScientist in forum New To Java
    Replies: 22
    Last Post: 08-30-2011, 11:09 AM
  4. Referencing object from another Thread
    By Floiancu in forum Threads and Synchronization
    Replies: 6
    Last Post: 05-15-2011, 01:20 PM
  5. referencing files in .exe
    By minime12358 in forum Advanced Java
    Replies: 7
    Last Post: 07-26-2010, 08:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •