Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPanel With JTextField or JLabel not updating

I have tried to find the answer but I'm coming up short. I am fairly new to java. I have 4 classes (1 main with a JFrame and 3 JPanels). The main class creates the JFrame and adds the 3 panels to it. What I am trying to accomplish is update a JLabel or JTextField in 1 panel (panelA) from an ActionEvent in another panel (panelB). The ActionEvent in panelB runs a method in panelA that runs the setText() method and the repaint() method. I cannot get the JLabel or JTextField to update with the new text.

Here is my code:

App.java

public class App {
    public static void main(String[] args) {
        JFrame nameFrame = new JFrame("Name Form");
        nameFrame.setLayout(new BorderLayout());
        nameFrame.setSize(300,150);
        nameFrame.setLocationRelativeTo(null);
        nameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MiddlePanel middlePanel = new MiddlePanel();
        nameFrame.add(middlePanel, BorderLayout.CENTER);
        BottomPanel bottomPanel = new BottomPanel();
        nameFrame.add(bottomPanel, BorderLayout.SOUTH);
        nameFrame.setVisible(true);
    }
}

BottomPanel.java

import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class BottomPanel extends JPanel {
    private JLabel welcomeLabel = new JLabel("Old Text");

    BottomPanel() {
        super(new FlowLayout());
        add(welcomeLabel);
    }
    public void setText(String text) {
        welcomeLabel.setText(text);
        repaint();
    }
}

MiddlePanel.java

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import java.awt.event.*;
import java.awt.Graphics;

public class MiddlePanel extends JPanel {
    MiddlePanel() {
        super(new BorderLayout());

        JButton okButton = new JButton("OK");
        okButton.setSize(20, 20);
        OKButtonListener okButtonListener = new OKButtonListener();
        okButton.addActionListener(okButtonListener);

        add(okButton, BorderLayout.EAST);
    }
    class OKButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            BottomPanel bottomPanel = new BottomPanel();
            bottomPanel.setText("New Text");
        }
    }
}

Thanks for any help.

like image 635
anonymous programmer Avatar asked Dec 22 '22 02:12

anonymous programmer


2 Answers

In your OKButtonListener you create a new instance of BottomPanel which has nothing to do with the BottomPanel you added to your JFrame. You will need the actual reference of the BottomPanel that you added in your JFrame.

like image 156
d1rk Avatar answered Jan 06 '23 14:01

d1rk


d1rk nailed it. Here is one way to achieve that effect.

End Result

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;

public class App {
    public static void main(String[] args) {
        JFrame nameFrame = new JFrame("Name Form");
        nameFrame.setLayout(new BorderLayout());
        nameFrame.setSize(300,150);
        nameFrame.setLocationRelativeTo(null);
        nameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        BottomPanel bottomPanel = new BottomPanel();

        MiddlePanel middlePanel = new MiddlePanel(bottomPanel);
        nameFrame.add(middlePanel, BorderLayout.CENTER);
        nameFrame.add(bottomPanel, BorderLayout.SOUTH);
        nameFrame.setVisible(true);
    }
}

class BottomPanel extends JPanel {
    private JLabel welcomeLabel = new JLabel("Old Text");

    BottomPanel() {
        super(new FlowLayout());
        add(welcomeLabel);
    }
    public void setText(String text) {
        welcomeLabel.setText(text);
        repaint();
    }
}

class MiddlePanel extends JPanel {

    private BottomPanel bottomPanel;

    MiddlePanel(BottomPanel bottomPanel) {
        super(new BorderLayout());

        this.bottomPanel = bottomPanel;

        JButton okButton = new JButton("OK");
        okButton.setSize(20, 20);
        OKButtonListener okButtonListener = new OKButtonListener();
        okButton.addActionListener(okButtonListener);

        add(okButton, BorderLayout.EAST);
    }
    class OKButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //BottomPanel bottomPanel = new BottomPanel();
            bottomPanel.setText("New Text");
        }
    }
}
like image 39
Andrew Thompson Avatar answered Jan 06 '23 14:01

Andrew Thompson