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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:58:44+00:00 2026-05-30T03:58:44+00:00

I am trying to build an MVC application in Java Swing. I have a

  • 0

I am trying to build an MVC application in Java Swing. I have a JPanel that contains four JComboBoxes and this JPanel is embedded into a parent JPanel. The parent JPanel has other controls in addition to the child JPanel.

The child JPanel’s model gets correctly updated whenever I change the values of the JComboBoxes (it’s basically a date picker with one combo box each for year, month, day of month, and hour of day). What I cannot figure out is how I can trigger the parent JPanel’s model to update itself to match the value stored in the child JPanel’s model whenever one of the JComboBoxes is changed.

Below is a stripped down SSCCE of the structure of what I have so far. Thank you.

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

public class Example extends JFrame {
    public Example() {
        super();
        OuterView theGUI = new OuterView();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        add(theGUI);
        pack();
        setVisible(true);        
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Example();
            }
        });        
    }
}

class OuterView extends JPanel {
    public OuterView() {
        super();
        InnerView innerPanel = new InnerView();
        JButton button = new JButton("display OuterView's model");
        button.addActionListener(new ButtonListener());
        add(innerPanel);
        add(button);
    }

    private class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent ae) {
            System.out.println("button was clicked");
        }
    }
}

class InnerView extends JPanel {
    public InnerView() {
        super();
        String[] items = new String[] {"item 1", "item 2", "item 3"};
        JComboBox comboBox = new JComboBox(items);
        comboBox.addActionListener(new ComboBoxListener());
        add(comboBox);
    }

    private class ComboBoxListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent ae) {
            String text = ((JComboBox) ae.getSource()).getSelectedItem().toString();
            System.out.println("store " + text + " in InnerView's model");
            System.out.println("now how do I cause OuterView's model to be updated to get the info from InnerView's model?");
        }        
    }
}
  • 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-30T03:58:45+00:00Added an answer on May 30, 2026 at 3:58 am

    You could use a PropertyChangeListener, and in fact one is built into every component. e.g.:

    import java.awt.event.*;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class Example extends JFrame {
       public Example() {
          super();
          OuterView theGUI = new OuterView();
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setResizable(false);
          add(theGUI);
          pack();
          setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                new Example();
             }
          });
       }
    }
    
    class OuterView extends JPanel {
       private String innerValue = "";
    
       public OuterView() {
          super();
          InnerView innerPanel = new InnerView();
          innerPanel.addPropertyChangeListener(new PropertyChangeListener() {
    
             @Override
             public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals(InnerView.COMBO_CHANGED)) {
                   innerValue = evt.getNewValue().toString();
                   System.out.println("new value from inside of OuterView: "
                         + innerValue);
                }
             }
          });
          JButton button = new JButton("display OuterView's model");
          button.addActionListener(new ButtonListener());
          add(innerPanel);
          add(button);
       }
    
       private class ButtonListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent ae) {
             System.out.println("button was clicked. innerValue: " + innerValue);
          }
       }
    }
    
    class InnerView extends JPanel {
       public static final String COMBO_CHANGED = "Combo Changed";
       // private SwingPropertyChangeSupport pcSupport = new
       // SwingPropertyChangeSupport(this);
       String oldValue = "";
    
       public InnerView() {
          super();
          String[] items = new String[] { "item 1", "item 2", "item 3" };
          JComboBox comboBox = new JComboBox(items);
          comboBox.addActionListener(new ComboBoxListener());
          add(comboBox);
    
       }
    
       private class ComboBoxListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent ae) {
             String text = ((JComboBox) ae.getSource()).getSelectedItem()
                   .toString();
             firePropertyChange(COMBO_CHANGED, oldValue, text);
             oldValue = text;
             System.out.println("store " + text + " in InnerView's model");
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to build a GUI application in Java/Swing. I'm mainly used to painting GUIs
I have an ASP.NET MVC application, with some RESTful services that I'm trying to
I'm trying to build a generic grid view in an ASP.NET MVC application. Let
I am trying to build a web application in ASP.NET MVC and need build
I have a forum like web application written in Asp.net MVC. I'm trying to
I'm trying to build an ASP.NET MVC 2 application. I want to pass data
I'm trying to build a global menu into my ASP.NET MVC site.master, and I
I am trying to build a simple drop down list for my MVC application.
I am working on an ASP.NET MVC application that contains a header and menu
im trying to build a application or a method that goes through all of

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.