Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 4.22 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. how to trigger an action in parent JPanel when a component in a child JPanel is updated (Java Swing)
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class Example extends JFrame {
  6.     public Example() {
  7.         super();
  8.         OuterView theGUI = new OuterView();
  9.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10.         setResizable(false);
  11.         add(theGUI);
  12.         pack();
  13.         setVisible(true);        
  14.     }
  15.  
  16.     public static void main(String[] args) {
  17.         SwingUtilities.invokeLater(new Runnable() {
  18.             public void run() {
  19.                 new Example();
  20.             }
  21.         });        
  22.     }
  23. }
  24.  
  25. class OuterView extends JPanel {
  26.     public OuterView() {
  27.         super();
  28.         InnerView innerPanel = new InnerView();
  29.         JButton button = new JButton("display OuterView's model");
  30.         button.addActionListener(new ButtonListener());
  31.         add(innerPanel);
  32.         add(button);
  33.     }
  34.  
  35.     private class ButtonListener implements ActionListener {
  36.         @Override
  37.         public void actionPerformed(ActionEvent ae) {
  38.             System.out.println("button was clicked");
  39.         }
  40.     }
  41. }
  42.  
  43. class InnerView extends JPanel {
  44.     public InnerView() {
  45.         super();
  46.         String[] items = new String[] {"item 1", "item 2", "item 3"};
  47.         JComboBox comboBox = new JComboBox(items);
  48.         comboBox.addActionListener(new ComboBoxListener());
  49.         add(comboBox);
  50.     }
  51.  
  52.     private class ComboBoxListener implements ActionListener {
  53.         @Override
  54.         public void actionPerformed(ActionEvent ae) {
  55.             String text = ((JComboBox) ae.getSource()).getSelectedItem().toString();
  56.             System.out.println("store " + text + " in InnerView's model");
  57.             System.out.println("now how do I cause OuterView's model to be updated to get the info from InnerView's model?");
  58.         }        
  59.     }
  60. }
  61.        
  62. import java.awt.event.*;
  63. import java.beans.PropertyChangeEvent;
  64. import java.beans.PropertyChangeListener;
  65.  
  66. import javax.swing.*;
  67.  
  68. @SuppressWarnings("serial")
  69. public class Example extends JFrame {
  70.    public Example() {
  71.       super();
  72.       OuterView theGUI = new OuterView();
  73.       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  74.       setResizable(false);
  75.       add(theGUI);
  76.       pack();
  77.       setVisible(true);
  78.    }
  79.  
  80.    public static void main(String[] args) {
  81.       SwingUtilities.invokeLater(new Runnable() {
  82.          public void run() {
  83.             new Example();
  84.          }
  85.       });
  86.    }
  87. }
  88.  
  89. class OuterView extends JPanel {
  90.    private String innerValue = "";
  91.  
  92.    public OuterView() {
  93.       super();
  94.       InnerView innerPanel = new InnerView();
  95.       innerPanel.addPropertyChangeListener(new PropertyChangeListener() {
  96.  
  97.          @Override
  98.          public void propertyChange(PropertyChangeEvent evt) {
  99.             if (evt.getPropertyName().equals(InnerView.COMBO_CHANGED)) {
  100.                innerValue = evt.getNewValue().toString();
  101.                System.out.println("new value from inside of OuterView: "
  102.                      + innerValue);
  103.             }
  104.          }
  105.       });
  106.       JButton button = new JButton("display OuterView's model");
  107.       button.addActionListener(new ButtonListener());
  108.       add(innerPanel);
  109.       add(button);
  110.    }
  111.  
  112.    private class ButtonListener implements ActionListener {
  113.       @Override
  114.       public void actionPerformed(ActionEvent ae) {
  115.          System.out.println("button was clicked. innerValue: " + innerValue);
  116.       }
  117.    }
  118. }
  119.  
  120. class InnerView extends JPanel {
  121.    public static final String COMBO_CHANGED = "Combo Changed";
  122.    // private SwingPropertyChangeSupport pcSupport = new
  123.    // SwingPropertyChangeSupport(this);
  124.    String oldValue = "";
  125.  
  126.    public InnerView() {
  127.       super();
  128.       String[] items = new String[] { "item 1", "item 2", "item 3" };
  129.       JComboBox comboBox = new JComboBox(items);
  130.       comboBox.addActionListener(new ComboBoxListener());
  131.       add(comboBox);
  132.  
  133.    }
  134.  
  135.    private class ComboBoxListener implements ActionListener {
  136.       @Override
  137.       public void actionPerformed(ActionEvent ae) {
  138.          String text = ((JComboBox) ae.getSource()).getSelectedItem()
  139.                .toString();
  140.          firePropertyChange(COMBO_CHANGED, oldValue, text);
  141.          oldValue = text;
  142.          System.out.println("store " + text + " in InnerView's model");
  143.       }
  144.    }
  145. }