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

  • Home
  • SEARCH
  • 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 7851251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:07:28+00:00 2026-06-02T19:07:28+00:00

I have a custom component that extends a JComponent. When you click it, it

  • 0

I have a custom component that extends a JComponent. When you click it, it catches the event with a MouseListener. Depending on the state of the component and where you click, the data stored in it updates.

How can I notify the parent container when this happens while keeping it completely self-contained?

  • 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-06-02T19:07:29+00:00Added an answer on June 2, 2026 at 7:07 pm

    Myself, I would use the PropertyChangeSupport that comes with JComponent, and then fire your notify method whenever the program’s state changes.

    For example:

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    
    import javax.swing.*;
    
    public class PropChangeEg {
       private static void createAndShowGui() {
          final JLabel counterLabel = new JLabel(" ", SwingConstants.CENTER);
    
          CustomComponent myCustomComponent = new CustomComponent();
          myCustomComponent.addPropertyChangeListener(new PropertyChangeListener() {
    
             @Override
             public void propertyChange(PropertyChangeEvent pcEvt) {
                if (pcEvt.getPropertyName().equals(CustomComponent.COUNTER)) {
                   String text = "Counter: " + pcEvt.getNewValue();
                   counterLabel.setText(text);
                }
             }
          });
    
          JFrame frame = new JFrame("PropChangeEg");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(myCustomComponent, BorderLayout.CENTER);
          frame.add(counterLabel, BorderLayout.PAGE_END);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    @SuppressWarnings("serial")
    class CustomComponent extends JComponent {
       public static final String COUNTER = "counter";
       private int counter = 0;
    
       public CustomComponent() {
          setLayout(new FlowLayout());
          add(new JButton(new AbstractAction("Increment Counter") {
    
             @Override
             public void actionPerformed(ActionEvent arg0) {
                setCounter(counter + 1);
             }
          }));
          add(new JButton(new AbstractAction("Decrement Counter") {
    
             @Override
             public void actionPerformed(ActionEvent arg0) {
                setCounter(counter - 1);
             }
          }));
       }
    
       //@Override // not needed!!
       //public void addPropertyChangeListener(PropertyChangeListener listener) {
       //   super.addPropertyChangeListener(listener);
       //}
    
       //@Override // not needed!!
       //public void removePropertyChangeListener(PropertyChangeListener listener) {
       //   super.removePropertyChangeListener(listener);
       //}
    
       public void setCounter(int counter) {
          int oldValue = this.counter;
          int newValue = counter;
          this.counter = newValue;
          firePropertyChange(COUNTER, oldValue, newValue);
       }
    
       public int getCounter() {
          return counter;
       }
    
    
    }
    

    Regarding comments:

    A ChangeListener will work fine too. 1+ to user129… but what I like about PCL is that I can specify the names of properties to listen for allowing me to listen to the changes of multiple different states and this way can create a complete View from a MVC design that is completely decoupled from the Control. So my recommendation is: if you’re only listening to one state, then by all means use a ChangeListener, but if you’re listening to multiple states, use a PCL.

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

Sidebar

Related Questions

I have created a Popup that is custom component extends Canvas i want the
I have a custom component that extends a panel. The panel has a top
I have created a Custom Event, that is fired from a custom component. It
I have custom component that I can place in my layout file (XML) for
I have a custom component that is vertically longer than the screen height and
I have a custom component, that contains a list of another components. If I
I have a component where i want to display a custom jtooltip. That is
I have problem with casting custom event currentTarget to component. When I'm trying to
I have a JTabbedPane with a custom tab component. That component contains a JLabel
I had a custom MXML component, that I have converted to a pure AS3

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.