JPanel With JTextField or JLabel not updated

I tried to find the answer, but I will come soon. I am new to java. I have 4 classes (1 main with JFrame and 3 JPanels). The main class creates a JFrame and adds 3 panels to it. What I'm trying to do is update the JLabel or JTextField panel to 1 (panel A) from an ActionEvent in another panel (panel B). The ActionEvent in panel B runs the method in panel A, which runs the setText () method and the repaint () method. I cannot get JLabel or JTextField to update with 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); } } 

Bottottpanel.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.

+4
source share
2 answers

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

+7
source

d1rk nailed it. Here is one way to achieve this 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"); } } } 
+4
source

Source: https://habr.com/ru/post/1395928/


All Articles