omo ativar uma ação no JPanel pai quando um componente em um JPanel filho é atualizado (Java Swin
Estou tentando criar um aplicativo MVC no Java Swing. Eu tenho um JPanel que contém quatro JComboBoxes e esse JPanel está incorporado em um JPanel pai. O JPanel pai possui outros controles além do JPanel filh
O modelo filho do JPanel é atualizado corretamente sempre que altero os valores dos JComboBoxes (é basicamente um seletor de datas com uma caixa de combinação cada para ano, mês, dia do mês e hora do dia). O que não consigo descobrir é como posso acionar o modelo do JPanel pai para atualizar-se para corresponder ao valor armazenado no modelo do JPanel filho sempre que um dos JComboBoxes for alterad
Abaixo é um SSCCE despojado da estrutura do que tenho até agora. Obrigado
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?");
}
}
}