Reputation: 41
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:
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);
}
}
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();
}
}
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.
Upvotes: 4
Views: 3587
Reputation: 168825
d1rk nailed it. Here is one way to achieve that effect.
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");
}
}
}
Upvotes: 4
Reputation: 1956
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.
Upvotes: 7