Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: JButton Auto-changing Reference Variable

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default JButton Auto-changing Reference Variable

    I want the reference variable for a JButton (say... b1) to automatically change with each iteration of a for loop. So:
    [CODE]
    for(int i=0;i<12;i++) {
      JButton b1 = mainButtonList.get(b1);
      //Code to edit each button specifically... (if statements)
      mainButtonList.set(i,b1);
      b1 = null;
    [/CODE]

    As you can see, there is only one reference variable right now so I'm not sure how I can really have any action listener figure out which button was pressed.

    getSource will return the reference variable. If they all are the same, how can you differentiate?

    What I am looking for is to change the reference used with each iteration. So maybe add some sort of b(i) statement so that the number next to be changes each time. But, I'm not sure if that would work since Java takes b(i) as the full reference variable if I'm not mistaken.

    Is this possible? If not, do you have any other suggestion?

    Thanks for your time!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JButton Auto-changing Reference Variable

    The value of b1 could be changed every time the assignment statement is executed:
    JButton b1 = mainButtonList.get(b1);
    depending on what the get() method returns.
    For each new value (a reference to a JButton) you can then use that reference to add a listener for the specific button that b1 points to at that time. The button objects referred to by b1 will have their contents changed to reflect the added listener. Then setting b1 to refer to a different button object will NOT change the contents of the button object that b1 pointed to before.

    how I can really have any action listener figure out which button was pressed.
    When an action event is created by a component (eg a JButton) the source field in the event object has a reference to the component that created the event(button pressed). The action listener can use getSource() to get that reference to that component.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: JButton Auto-changing Reference Variable

    Quote Originally Posted by Norm View Post
    When an action event is created by a component (eg a JButton) the source field in the event object has a reference to the component that created the event(button pressed). The action listener can use getSource() to get that reference to that component.
    But the problem there is that if the reference for b1 keeps getting changed, it's value is set to the last assignment before the button gets pressed. So if any of the other buttons are pressed, they will return exceptions because their references are null.

    Do I need to use a button group? I'm having trouble understanding how to use it from the API.

    EDIT: Scratch the button group. They can't be used with JButtons.
    Last edited by bgroenks96; June 19th, 2011 at 01:38 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JButton Auto-changing Reference Variable

    The button objects that were pointed to by b1 (each in turn) still exist. Changing the value of b1 does NOT change the existence of the object that it pointed to. If the buttons are added to a GUI, then the GUI has references to the buttons, no matter what value is in b1. b1 is a temporary reference that can be set to null when the code is done adding the buttons to the GUI and adding listeners to the button.
    When the button is pressed, the button's object has a reference to the listener and will call its method passing a reference to itself in the event object. getSource() will get that reference.

    Write some code and test it. Then you'll know. Either it works or not. If not come back and post the errors.

  5. The Following User Says Thank You to Norm For This Useful Post:

    bgroenks96 (June 19th, 2011)

  6. #5
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: JButton Auto-changing Reference Variable

    Quote Originally Posted by Norm View Post
    The button objects that were pointed to by b1 (each in turn) still exist. Changing the value of b1 does NOT change the existence of the object that it pointed to. If the buttons are added to a GUI, then the GUI has references to the buttons, no matter what value is in b1. b1 is a temporary reference that can be set to null when the code is done adding the buttons to the GUI and adding listeners to the button.
    When the button is pressed, the button's object has a reference to the listener and will call its method passing a reference to itself in the event object. getSource() will get that reference.

    Write some code and test it. Then you'll know. Either it works or not. If not come back and post the errors.
    You were right. Thanks for your help.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JButton Auto-changing Reference Variable

    Many times its faster to write some test code and see what happens.

    But you should always read the API doc so you know what the choices are.

  8. #7
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: JButton Auto-changing Reference Variable

    Quote Originally Posted by Norm View Post
    Many times its faster to write some test code and see what happens.

    But you should always read the API doc so you know what the choices are.
    Yeah the API is my second Bible. I could read it all day...

    I just thought that by reassigning the same reference variable you leave the previously assigned object with a reference of null.

Similar Threads

  1. Auto Select
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 14
    Last Post: May 31st, 2011, 04:55 AM
  2. cannot find symbol in reference variable
    By NPotter86 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 08:21 PM
  3. Auto Fill the TextField
    By malladiG in forum Java Theory & Questions
    Replies: 0
    Last Post: April 6th, 2010, 07:32 AM
  4. Auto contrast and auto brightness
    By oxxxis in forum Java Theory & Questions
    Replies: 0
    Last Post: January 21st, 2010, 03:00 PM
  5. Fitting a large primitive into a small reference variable
    By Phobia in forum Java Theory & Questions
    Replies: 15
    Last Post: October 23rd, 2009, 03:10 PM