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 7497005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:53:17+00:00 2026-05-29T18:53:17+00:00

Working in Java: I have a JFrame class, and separate classes for my two

  • 0

Working in Java: I have a JFrame class, and separate classes for my two JPanels that are added to the JFrame. One of the JPanel classes has some buttons in it, which can interact with each other(when I click on one button, it can disable another button). However, I can’t figure out how to get the button to call a method in the other JPanel (written in a separate class).

So, my program look like this:

JFrame

  • Jpanel1
  • Jpanel2 – This class has my buttons in it, I’m trying to get them to interact with the JPanel1 object.

Any tips appreciated, thanks!

  • 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-29T18:53:19+00:00Added an answer on May 29, 2026 at 6:53 pm

    One way to do this is to pass an instance of (to use your terminology) Jpanel1 into Jpanel2. This doesn’t have to be done in the constructor, you can have a setConnectedPanel(JPanel) method, for example.

    Here’s some code that demonstrates what you want to do:

    MyFrame.java

    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class MyFrame extends JFrame {
    
      public MyFrame() {
        ReactionPanel rp = new ReactionPanel();
        ActionPanel ap = new ActionPanel(rp);
        setLayout(new GridLayout(2, 1));
        add(ap);
        add(rp);
        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
            new MyFrame();
          }
        });
      }
    }
    

    ActionPanel.java

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    public class ActionPanel extends JPanel implements ActionListener {
      private ReactionPanel rp;
      private JButton button;
    
      public ActionPanel(ReactionPanel rp) {
        this.rp = rp;
        button = new JButton("Click");
        button.addActionListener(this);
        this.add(button);
      }
    
      @Override
      public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(button)) {
          rp.react();
        }
      }
    }
    

    ReactionPanel.java

    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class ReactionPanel extends JPanel {
      private JLabel label;
    
      public ReactionPanel() {
        label = new JLabel("PING");
        this.add(label);
      }
    
      public void react() {
        if(label.getText().equals("PING")) {
          label.setText("PONG");
        } else {
          label.setText("PING");
        }
      }
    }
    

    As you can see, I tend to override all of my JFrames/JPanels when I write Swing GUIs as I find it easier and more flexible but YMMV.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say that I in Java have 3 classes, wheres the super one has a
I'm working in Java, and I have a JPanel in a JFrame. In that
I have a Graphics object of JPanel and that is working fine: import java.awt.Color;
I'm working with java, trying to use the AudioFormat Class. I have the following
I have been working on a Java project for a class for a while
I have a working exec(java $file) command in php, but the problem is that
I have been working with Java's HashMap lately and have run afoul of some
I have been working with Java a couple of years, but up until recently
I have been working in Java/J2ee projects, in which I follow the Maven structure.
I have created a couple of java working sets for a project in my

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.