wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Which JPanel is the button on 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 "Which JPanel is the button on" Watch "Which JPanel is the button on" New topic
Author

Which JPanel is the button on

Michael Labuschagne
Ranch Hand

Joined: May 08, 2007
Posts: 56

I've got two JPanels (A and B) and One JButton. I add the button to both JPanels. When it is clicked how would I know if it was on JPanel A or B... Been stumping me for at least two hours...

Thanks for any help!
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Michael Labuschagne:
I've got two JPanels (A and B) and One JButton. I add the button to both JPanels. When it is clicked how would I know if it was on JPanel A or B...


You can't add the same JButton to two JPanels. Or rather, adding it to the second one will remove it from the first. Should I presume that you are talking about two identical JButtons added to two JPanels? In that case:

  • You could add a different listener to each JButton.
  • You could call setActionCommand() with different arguments on each button, then examine yourActionEvent.getActionCommand().
  • You could compare yourActionEvent.getSource() for equality with references to each of your two JButtons.
  • You could examine the result of something like ((Component)yourActionEvent.getSource()).getParent().


  • [ November 28, 2007: Message edited by: Brian Cole ]

    bitguru blog
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19654
        
      18



    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Michael Labuschagne
    Ranch Hand

    Joined: May 08, 2007
    Posts: 56

    Originally posted by Brian Cole:


    You can't add the same JButton to two JPanels. Or rather, adding it to the second one will remove it from the first. Should I presume that you are talking about two identical JButtons added to two JPanels? In that case:

  • You could add a different listener to each JButton.
  • You could call setActionCommand() with different arguments on each button, then examine yourActionEvent.getActionCommand().
  • You could compare yourActionEvent.getSource() for equality with references to each of your two JButtons.
  • You could examine the result of something like ((Component)yourActionEvent.getSource()).getParent().


  • [ November 28, 2007: Message edited by: Brian Cole ]


    I'm pretty sure it's the same object in memory. I create one JButton (I add an actionListener from the class the JButton is instantiated in, I pass this JButton to constructor of a different class which is instantiated twice. The class the JButton is passed to extends JPanel... I take these JPanel and place them in another JPanel for display. Which ever button I click on, from JPanel A or JPanel B the same code executed that I defined in the originating class.

    This would lead me to believe that one JButton is being added to two different containers at the same time, or have I misunderstood?
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19654
        
      18

    You have misunderstood: any component can have only one parent.

    If you want the "same" button on both panels, you probably should switch to an Action instead. This action can be used for multiple buttons, and if you disable the action you disable all buttons (and menu items, etc) that use the action.
    Michael Labuschagne
    Ranch Hand

    Joined: May 08, 2007
    Posts: 56

    Originally posted by Rob Prime:
    You have misunderstood: any component can have only one parent.

    If you want the "same" button on both panels, you probably should switch to an Action instead. This action can be used for multiple buttons, and if you disable the action you disable all buttons (and menu items, etc) that use the action.


    Ok I could understand that... I did some investigating though. Both buttons have the same hash code (not sure if that's conclusive enough to prove that they're the same button/object) and when I try and get the parent object from either of the buttons I just get null back.

    So perhaps a button cannot have two parent components, it would seem as though they can be placed in two containers... But then again logically, if they don't have parent objects how can they be placed in the JPanels... Weird!
    Brian Cole
    Author
    Ranch Hand

    Joined: Sep 20, 2005
    Posts: 862
    Originally posted by Michael Labuschagne:
    Both buttons have the same hash code (not sure if that's conclusive enough to prove that they're the same button/object)


    It's not. Now if they had the same value for System.identityHashCode() that would be more persuasive (but still not absolutely conclusive).

    [edit: I take this back, since JButton inherits hashCode() from Object. So I guess I'm willing to believe the two buttons with the same hash codes are the same instance, especially since you show below that neither has been added to a panel.

    In general, though, use System.identityHashCode(), not hashCode(), if you want to determine if two objects are the same instance (and you can't use the == operator) and be prepared for the rare hash collision.]

    and when I try and get the parent object from either of the buttons I just get null back.


    Then it would seem that neither button has been has been added to either of the panels. In that case, I guess they could be the same button instance.

    Perhaps there's some clone()ing going on behind your back or something?

    So perhaps a button cannot have two parent components, it would seem as though they can be placed in two containers...


    No, they can't. If you need to be convinced, take a look at the source of Container.addImpl(). You'll see it does this:

    [ November 29, 2007: Message edited by: Brian Cole ]
    Michael Labuschagne
    Ranch Hand

    Joined: May 08, 2007
    Posts: 56

    Originally posted by Brian Cole:


    No, they can't. If you need to be convinced, take a look at the source of Container.addImpl(). You'll see it does this:


    [ November 29, 2007: Message edited by: Brian Cole ]


    Thanks for the input Brian, very interesting!!
     
     
    subject: Which JPanel is the button on
     
    Similar Threads
    Linking two JPanels with a JButton
    Applet with sliding buttons.
    JScrollPane impossible to add....
    Scrollbar not Working
    Applying affine transform to JPanel