0

I am new on Java.

I have developed an application with some different JPanels (using a BorderLayout, 3 panels in this case).

In panel 1, I have a JLabel and a variable (a class) that is related with its value (method get); in panel 2, I updated the value of the variable (method set) because it is done when an action is performed in this second panel.

How Could I get the value of the JLabel in panel 1 updated?

I don't know how to trigger an event or something similar after updating the value from panel 2 and how to make panel 1 to listen to this change.

Let me explain a bit more. I have a JFrame with two JPanels and I update the model from one panel. Once the model is updated, the JLabel from the other JPanel should be updated: Main: JFrame

public class MainClass extends JFrame
{
    public MainClass()
    {
        // JPanel 1
        ....        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400,300);
        setLocationRelativeTo(null);
        setTitle("Test");
        setResizable(false);
        setVisible(true);
        // JPanel 1
        this.add(west, BorderLayout.WEST);
        // JPanel 2
        this.add(board, BorderLayout.CENTER);
    }

    public static void main(String[] args)
    {
        // put your code here
        new MainClass ();
    }
}

JPanel 1

public class West extends JPanel
{
    contFase = new Contador(titulo, valor);
    JLabel lblTitulo;
    ...
    lblTitulo.setText = contFase.getText();
    this.add(lblTitulo);
    ...
}

JPanel 2

public class Board extends JPanel implements ActionListener
{
    ....
    public void actionPerformed(ActionEvent e)
    {
        ...
        //Here Label of panel 1 should be updated with the model
        contFase.setValor(contFase.getValor() + pacman.comerElemento(fase.getPacdots(), fase.getPowerPellets()));
        ...
    }
}
Agus P.
  • 3
  • 1
  • 3

1 Answers1

1

I have little idea how your code looks like because you didn't show any, but here is an example of how to edit a JLabel when an action is taken (in this case - pressing a button). The layout of the components on panels does not matter, but I put 2 panels like you wanted.

public class ValueUpdate extends JFrame {

    int x = 0;
    final JLabel label = new JLabel(String.valueOf(x));

    ValueUpdate() {

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();

        panel1.add(label);

        JButton btn = new JButton("Increment");
        btn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                x++;
                label.setText(String.valueOf(x));
            }
        });

        panel2.add(btn);

        getContentPane().add(panel1, BorderLayout.CENTER);
        getContentPane().add(panel2, BorderLayout.PAGE_END);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {

        new ValueUpdate();
    }
}
user1803551
  • 12,965
  • 5
  • 47
  • 74
  • 1
    Maybe simply make JPanel1 managed from JFrame and make an update from JPanel2 to the JFrame? (to update JPanel1) – Agus P. Apr 29 '14 at 08:52
  • @AgusP. I don't understand what you mean by making a component manage another component. Right now a button in `panel2` changes a label in`panel1`. They are both inside the `JFrame` which holds the value one is changing and the other is displaying. – user1803551 Apr 29 '14 at 08:58
  • Ok, let me explain. I have not a "Component" that raise one ActionPerformed. Really, this is a "game" and in JPanel2 I have a Timer. so, there is a case in which the main character do something (in panel 2) and then the label in JPanel1 the label should be updated. I think that it should be similar to your logic but maybe is more that in my architecture something is wrong and every change should be done from "MainClass". Any suggest? – Agus P. Apr 29 '14 at 11:14
  • 1
    @AgusP. `actionPerformed` **must** be called by a component because you must register the `ActionListener` to a component. It does not matter in which panels your components are. Post an [MCVE](http://stackoverflow.com/help/mcve) because your code shows us nothing to go with. – user1803551 Apr 29 '14 at 23:22
  • Thanks, really, answering to my question, it is. – Agus P. May 02 '14 at 21:20