1

First off, I'm new to Java, so please be gentle.

I have a JFrame which has two 'JPanels', one of which is a separate class (i've extended the JPanel). Ideally I'd like to 'dispatch and event' or notify the other JPanel object on the JFrame.

I have an array of JButtons in the custom JPanel class, which i'd like to add an event listener to. Upon clicking the JButton, i'd like to change something on the other JPanel.

I'm really not sure how to do this, so I moved the event handler into the JFrame class, and tried to do the following:

panel.buttonArray[i][j].addActionListener(this);

However, doing that didn't work at all. Annoyingly, Eclipse didn't complain either...

Any tips on how I can achieve this?

This was horribly explained, sorry.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Jonathan
  • 61
  • 1
  • 6
  • 1
    Ok, we're gonna name your objects. Your JFrame = frame. Your custom JPanel = mypanel. Your other JPanel = panel2. What exactly do you want to do to panel2? – Perry Monschau Mar 18 '12 at 16:16
  • `extended the JPanel` Why, what methods are overridden? – Andrew Thompson Mar 18 '12 at 16:16
  • Does you class implement `ActionsListener`? Have you implemented the `actionPerformed` method? – assylias Mar 18 '12 at 16:16
  • @PerryMonschau The frame is split into two, with the panels on either side. Panel1, has a 'checkerboard' made up of jButtons. Upon clicking any of the buttons in panel1 I'd like to update a jlabel in panel 2. – Jonathan Mar 18 '12 at 16:24
  • 1
    @AndrewThompson It doesn't override any methods, I was simply using it as a way to keep things together. I.e. i've named it Board, as it's a 'checkerboard'. – Jonathan Mar 18 '12 at 16:24
  • 1
    "Eclipse" isn't going to "complain" if your code compiles. Just because it compiles doesn't mean that it will behave properly (not until your bugs are fixed), so best not get annoyed at this or you will be annoyed a lot. You've likely got a problem with references as Chuck Fricano explains. Also, look into using the observer design pattern so that one object gets *notified* of changes in another object when the changes occur. All Swing listeners are based on this. – Hovercraft Full Of Eels Mar 18 '12 at 16:26
  • *"It doesn't override any methods.."* Then just create an instance of a `JPanel`, there is no need to extend classes unless changing behavior (methods). – Andrew Thompson Mar 18 '12 at 16:31
  • @AndrewThompson, well I guess I have over-riden it, as i've changed the constructer. Heigh ho. That's beside the point. – Jonathan Mar 18 '12 at 16:32
  • @Jonathan: better to go back to the basics :) -- you cannot override a constructor. Ever. Again, you need to have references of one object held by another. To help you more, you may need to tell us more. – Hovercraft Full Of Eels Mar 18 '12 at 17:09

1 Answers1

2

Think of this not in terms of panels but in terms of objects. As long an object, lets say it has a name of object77, has a reference to the other object, call it object42, object77 can call methods on object42.

  object77.methodInObject42();

  panel77.methodInPanel42();

As for the event handler, then

   buttonOnPanelXX.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e) {
             panel77.methodInPanel42();
       }});

or even better...

    button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            SwingUtilities.invokeLater(new Runnable(){
               public void run() {
                  panel77.methodInPanel42();
               }});
            }});
Java42
  • 7,628
  • 1
  • 32
  • 50
  • I'm really not sure how i'd implement this. How can a child object call a method in a child of its parent? (I think thats what it is) Maybe I should draw a diagram lol. – Jonathan Mar 18 '12 at 16:50
  • Edit: That didn't come out as hoped. [link](http://bit.ly/yVL6R6) There is what I mean, the buttons are declaed inside panel1, and need to modify panel2 – Jonathan Mar 18 '12 at 16:59
  • @Jonathan - have jframe call a panel1 method and pass panel2. ( panel1.identify(panel2); ) – Java42 Mar 18 '12 at 18:59
  • I worked it out, thanks! :) Did it a slightly different way, but you gave me the right pointers (hah!). _Must learn that there are no pointers in java_ – Jonathan Mar 18 '12 at 19:42