-1

I've been researching communication, event handling and listening across JPanels for a while. I'm going to try and describe my issue without code first, because I feel it's more a design pattern roadblock.

So I have three custom JPanels inside a custom JFrame, each with their own instance variables and Actionlisteners. The ActionListeners at the moment update variables whenever a change happens within them.

But the catch is, I want the ActionListener in one panel to pay attention to elements in other panels. So if I've a box in Panel B and I change its value, I want the label in Panel C to change too.

I've researched a lot on the topic, from stackoverflow answers to documentation on the topic. But I'm having a hard time putting it all together. Especially when I've divided my custom panels into different classes. Can anyone help sum up how it should look?

Sammieo
  • 155
  • 1
  • 7
  • 1
    Your question is broad, quite possibly overly broad, and about the best anyone can say to you is to look at and study the Model-View-Controller and to follow this. – Hovercraft Full Of Eels Sep 30 '17 at 01:09
  • 1
    And that other question that you linked to (and which I answered) is a much better question than yours, since it showed some real code. Please consider doing the same. Don't post all your code, just a decent attempt at a [mcve] representation of it. – Hovercraft Full Of Eels Sep 30 '17 at 01:10

1 Answers1

1

Ultimately what you need here is to register an ActionListener on Panel B's text box which updates Panel C's label. The fact that Panel B and Panel C are different classes is just a minor bump in the road. The code that sets up this ActionListener simply needs to be able to get hold of references to 1) the text field whose actions we are interested in observing, and 2) the label whose text we are interested in changing.

Now, if Panel B and Panel C weren't separate classes, we would probably just have references to the text field and label handily laying around in member variables of our JFrame window. But Panel B and Panel C are separate classes, so we'll need to ask for their help. Well, actually, not so much ask as demand by dint of a little reprogramming...

First, have Panel B expose the text field with a getter method:

public class PanelB extends JPanel {
    // ...
    private JTextField textBox;
    // ...
    public JTextField getTextBox(){
        return textBox;
    }
}

Then, expose Panel C's label with a getter method:

class PanelC extends JPanel {
    // ...
    private JLabel label;
    // ...
    public JLabel getLabel() {
        return label;
    }
}

Now you can set up an ActionListener in more or less the usual way:

class MyFrame extends JFrame {

    PanelB panelB = new PanelB();
    PanelC panelC = new PanelC();

    public MyFrame()
    {
        // ...
        final JTextField panelBtf = panelB.getTextBox();
        final JLabel panelClabel = panelC.getLabel();
        panelBtf.addActionListener(
            new ActionListener() {
            public void actionPerformed(ActionEvent ae)
            {
                panelClabel.setText(panelBtf.getText());
            }}
        );
    }
}    
Kevin Anderson
  • 4,568
  • 3
  • 13
  • 21
  • Ah, I think I see. So it's about exposing the elements within those panels to the frame class and then implementing action listeners there? That makes a lot of sense if so; my mistake was containing all the panel elements and their listeners inside their respective classes, meaning they had no way of seeing each other. I'd feel like an idiot for not seeing so earlier if I weren't so grateful. Thank you very much, the way you presented your answer and your use of examples was exactly what I needed. I won't be able to try this for another six hours and confirm, but I look forward to doing so. – Sammieo Sep 30 '17 at 02:47
  • @Sammieo: why have you not posted code as requested? – DontKnowMuchBut Getting Better Sep 30 '17 at 02:50