Question

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()));
        ...
    }
}
Was it helpful?

Solution

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();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top