2012-02-19 9 views
7

Sto provando a creare un'applicazione MVC in Java Swing. Ho un JPanel che contiene quattro JComboBoxes e questo JPanel è incorporato in un JPanel padre. Il genitore JPanel ha altri controlli oltre al bambino JPanel.come attivare un'azione nel padre JPanel quando viene aggiornato un componente in un bambino JPanel (Java Swing)

Il modello di bambino JPanel viene aggiornato correttamente ogni volta che cambio i valori dei JComboBoxes (è fondamentalmente un raccoglitore di date con una casella combinata per anno, mese, giorno del mese e ora del giorno). Quello che non riesco a capire è come posso attivare il modello genitore di JPanel per aggiornarsi per abbinare il valore memorizzato nel modello JPanel del bambino ogni volta che uno dei JComboBoxes viene cambiato.

Di seguito è riportato un SSCCE ridotto della struttura di ciò che ho finora. Grazie.

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?"); 
     }   
    } 
} 
+0

Il genitore dovrebbe avere un listener sul modello del bambino. –

+0

Oppure puoi _forward_ l'evento con il genitore, come mostrato [qui] (http://stackoverflow.com/q/2159803/230513). – trashgod

risposta

15

È possibile utilizzare un PropertyChangeListener, e infatti uno è integrato in tutti i componenti. ad esempio

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"); 
     } 
    } 
} 
+1

+1 per accoppiamento lento. – trashgod

+0

Grazie, la tua soluzione è stata davvero utile! – user1002119

+0

@ user1002119: prego! Sono contento che ci abbia aiutato. –

Problemi correlati