Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6348351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:24:10+00:00 2026-05-24T21:24:10+00:00

I have a Java JFrame containing a JPanel. Within that JPanel, there are two

  • 0

I have a Java JFrame containing a JPanel. Within that JPanel, there are two separate JPanels. When the user clicks a button in the first JPanel, a message needs to be sent to the other JPanel notifying it which button was clicked. What is the easiest way to send messages between objects like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:24:11+00:00Added an answer on May 24, 2026 at 9:24 pm

    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();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Java application which adds JTextFields @ runtime to JPanel. Basically user clicks
I'm working in Java, and I have a JPanel in a JFrame. In that
Java Newbie here. I have a JFrame that I added to my netbeans project,
I have this class called PollFrame that extends JFrame in a file called PollFrame.java
I have a JFrame that displays a Java icon on the title bar (left
The scenario is this: I have Java swing application with JFrame. There is textarea
I have a non-GUI thread that starts a JFrame using java.awt.EventQueue.invokeLater(new Runnable() { public
I have a main.java that has a button, when you press it, it calls
I have written a java applet which opens a JFrame (so when run in
I have Java Map (out of Strings and Ints) objects that I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.