Sending messages between two JPanel objects

For mKorbel (and the original poster):
What I’m recommending is looser coupling, that the one JPanel has no knowledge of the other JPanel and that all connections are done through a control of some sort. For instance, to borrow some of your code:

CopyTextNorthPanel2.java

import java.awt.*;
import javax.swing.*;

public class CopyTextNorthPanel2 extends JPanel {

   private static final long serialVersionUID = 1L;
   public JTextField northField;

   public CopyTextNorthPanel2() {
      northField = new JTextField("Welcome World");
      northField.setFont(new Font("Serif", Font.BOLD, 20));
      northField.setPreferredSize(new Dimension(300, 25));
      add(northField);
   }

   public String getNorthFieldText() {
      return northField.getText();
   }
}

CopyTextSouthPanel2.java

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

public class CopyTextSouthPanel2 extends JPanel {

   private static final long serialVersionUID = 1L;
   private JTextField firstText = new JTextField("Desired TextField");
   private JButton copyButton = new JButton("Copy text from JTextFields");
   private CopyTextControl2 control;

   public CopyTextSouthPanel2() {
      copyButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            if (control != null) {
               control.copyAction();
            }
         }
      });

      add(firstText);
      add(copyButton);
   }

   public void setControl(CopyTextControl2 control) {
      this.control = control;
   }

   public void setFirstText(String text) {
      firstText.setText(text);
   }
}

CopyTextControl2.java

public class CopyTextControl2 {
   private CopyTextNorthPanel2 northPanel;
   private CopyTextSouthPanel2 southPanel;

   public void copyAction() {
      if (northPanel != null && southPanel != null) {
         southPanel.setFirstText(northPanel.getNorthFieldText());
      }
   }

   public void setNorthPanel(CopyTextNorthPanel2 northPanel) {
      this.northPanel = northPanel;
   }

   public void setSouthPanel(CopyTextSouthPanel2 southPanel) {
      this.southPanel = southPanel;
   }

}

CopyText2.java

import java.awt.*;
import javax.swing.*;

public class CopyText2 {

   private static void createAndShowUI() {
      CopyTextNorthPanel2 northPanel = new CopyTextNorthPanel2();
      CopyTextSouthPanel2 southPanel = new CopyTextSouthPanel2();
      CopyTextControl2 control = new CopyTextControl2();

      southPanel.setControl(control);
      control.setNorthPanel(northPanel);
      control.setSouthPanel(southPanel);

      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(northPanel, BorderLayout.NORTH);
      mainPanel.add(Box.createRigidArea(new Dimension(100, 100)), BorderLayout.CENTER);
      mainPanel.add(southPanel, BorderLayout.SOUTH);

      JFrame frame = new JFrame("Copy Text");
      frame.getContentPane().add(mainPanel);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

Leave a Comment