Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2010
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.84 KB | None | 0 0
  1. package intro;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.beans.BeanInfo;
  8. import java.beans.Beans;
  9. import java.beans.IntrospectionException;
  10. import java.beans.Introspector;
  11. import java.beans.PropertyDescriptor;
  12. import java.io.IOException;
  13. import java.lang.reflect.InvocationTargetException;
  14. import java.lang.reflect.Method;
  15. import javax.swing.JTextField;
  16.  
  17. public class JFrameExt extends JFrame implements ActionListener {
  18.  
  19.     private static final long serialVersionUID = 1L;
  20.     private JPanel jContentPane = null;
  21.     private JPanel jpLeft = null;
  22.     private JPanel jpRight = null;
  23.     private JPanel jpController = null;
  24.     private JPanel jpInspector = null;
  25.     private JComboBox jcboMenu = null;
  26.     private JPanel jpPropNames = null;
  27.     private JPanel jpPropValues = null;
  28.     //Create an array of 10 JLabels
  29.     private JLabel[] jlbPropNames = new JLabel[10];
  30.     //Create an array of 10 JTextFields
  31.     private JTextField[] jtfPropValues = new JTextField[10];
  32.     private JPanel targetBeanObject = null;
  33.     private Class classObject = null;
  34.     private PropertyDescriptor[] pd = null;
  35.     /**
  36.      * This method initializes jpLeft  
  37.      *  
  38.      * @return javax.swing.JPanel  
  39.      */
  40.     private JPanel getJpLeft() {
  41.         if (jpLeft == null) {
  42.             jpLeft = new JPanel();
  43.             jpLeft.setLayout(new GridBagLayout());
  44.         }
  45.         return jpLeft;
  46.     }
  47.  
  48.     /**
  49.      * This method initializes jpRight 
  50.      *  
  51.      * @return javax.swing.JPanel  
  52.      */
  53.     private JPanel getJpRight() {
  54.         if (jpRight == null) {
  55.             jpRight = new JPanel();
  56.             jpRight.setLayout(new BorderLayout());
  57.             jpRight.add(getJpController(), BorderLayout.NORTH);
  58.             jpRight.add(getJpInspector(), BorderLayout.CENTER);
  59.         }
  60.         return jpRight;
  61.     }
  62.  
  63.     /**
  64.      * This method initializes jpController
  65.      *  
  66.      * @return javax.swing.JPanel  
  67.      */
  68.     private JPanel getJpController() {
  69.         if (jpController == null) {
  70.             GridBagConstraints gridBagConstraints = new GridBagConstraints();
  71.             gridBagConstraints.fill = GridBagConstraints.VERTICAL;
  72.             gridBagConstraints.weightx = 1.0;
  73.             jpController = new JPanel();
  74.             jpController.setLayout(new GridBagLayout());
  75.             jpController.add(getJcboMenu(), gridBagConstraints);
  76.         }
  77.         return jpController;
  78.     }
  79.  
  80.     /**
  81.      * This method initializes jpInspector 
  82.      *  
  83.      * @return javax.swing.JPanel  
  84.      */
  85.     private JPanel getJpInspector() {
  86.         if (jpInspector == null) {
  87.             GridLayout gridLayout1 = new GridLayout();
  88.             gridLayout1.setRows(1);
  89.             gridLayout1.setColumns(2);
  90.             jpInspector = new JPanel();
  91.             jpInspector.setLayout(gridLayout1);
  92.             jpInspector.add(getJpPropNames(), null);
  93.             jpInspector.add(getJpPropValues(), null);
  94.         }
  95.         return jpInspector;
  96.     }
  97.  
  98.     /* method initializing jcboMenu combo box */
  99.     private JComboBox getJcboMenu() {
  100.         if (jcboMenu == null) {
  101.             jcboMenu = new JComboBox();
  102.             jcboMenu.setEditable (false);
  103.             jcboMenu.addActionListener(new java.awt.event.ActionListener() {
  104.                 public void actionPerformed(java.awt.event.ActionEvent e) {
  105.                     // draw the selected item
  106.                     jcboClassName_actionPerformed(e);
  107.                 }
  108.             });
  109.             jcboMenu.addItem("");
  110.             jcboMenu.addItem("intro.Rect");
  111.             jcboMenu.addItem("intro.Circ");
  112.             jcboMenu.addItem("intro.Ticker");
  113.         }
  114.         return jcboMenu;
  115.     }
  116.  
  117.     /**
  118.      * This method initializes jpPropNames 
  119.      *  
  120.      * @return javax.swing.JPanel  
  121.      */
  122.     private JPanel getJpPropNames() {
  123.         if (jpPropNames == null) {
  124.             GridLayout gridLayout2 = new GridLayout();
  125.             gridLayout2.setRows(10);
  126.             gridLayout2.setColumns(1);
  127.             jpPropNames = new JPanel();
  128.             jpPropNames.setLayout(gridLayout2);
  129.         }
  130.         return jpPropNames;
  131.     }
  132.  
  133.     /**
  134.      * This method initializes jpPropValues
  135.      *  
  136.      * @return javax.swing.JPanel  
  137.      */
  138.     private JPanel getJpPropValues() {
  139.         if (jpPropValues == null) {
  140.             GridLayout gridLayout3 = new GridLayout();
  141.             gridLayout3.setRows(10);
  142.             gridLayout3.setColumns(1);
  143.             jpPropValues = new JPanel();
  144.             jpPropValues.setLayout(gridLayout3);
  145.         }
  146.         return jpPropValues;
  147.     }
  148.  
  149.     public static void main(String[] args) {
  150.        
  151.         SwingUtilities.invokeLater(new Runnable() {
  152.             public void run() {
  153.                 JFrameExt thisClass = new JFrameExt();
  154.                 thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  155.                 thisClass.setVisible(true);
  156.             }
  157.         });
  158.     }
  159.  
  160.     /**
  161.      * This is the default constructor
  162.      */
  163.     public JFrameExt() {
  164.         super();
  165.         initialize();
  166.            
  167.         //Creating JLabel objects one by one in a loop
  168.         for (int j = 0; j < 10; j++) {
  169.             //Create JLabel object
  170.             jlbPropNames[j] = new JLabel("");
  171.             //Add JLabel object to JPanel
  172.             jpPropNames.add(jlbPropNames[j]);
  173.         }
  174.         //Creating JTextField objects one by one in a loop
  175.         for (int j = 0; j < 10; j++) {
  176.             //Create JTextField objects. Each of width 10
  177.             jtfPropValues[j] = new JTextField();
  178.                                    
  179.             //Register this as the listener with JTextField object
  180.             jtfPropValues[j].addActionListener(new java.awt.event.ActionListener() {
  181.                 public void actionPerformed(java.awt.event.ActionEvent e) {
  182.                     // reflect changes
  183.                     jtfActionPerformed(e);
  184.                 }
  185.             });
  186.                        
  187.             //Add JTextField object to JPanel
  188.             jpPropValues.add(jtfPropValues[j]);
  189.         }
  190.     }
  191.    
  192.     /** drop down event begin*/
  193.     void jcboClassName_actionPerformed(ActionEvent e)
  194.  
  195.       {
  196.  
  197.         JPanel targetBeanObject = null;
  198.         //Get target bean class name from combo box
  199.         String className = (String)jcboMenu.getSelectedItem();
  200.         if (className.equals(""))
  201.             return;
  202.  
  203.         //Create target bean object using bean class name.
  204.         try {
  205.             targetBeanObject = (JPanel)Beans.instantiate(null, className);
  206.         }
  207.  
  208.         catch (ClassNotFoundException ex) {
  209.         }
  210.  
  211.         catch (IOException ex) {
  212.         }
  213.  
  214.         //If targetBeanObject is a JPanel,
  215.         //remove jpLeft and replace it with targetBeanObject
  216.         //and validate (refresh) the content pane.
  217.         if (targetBeanObject instanceof JPanel) {
  218.             jContentPane.remove(0); //remove component at index 0
  219.             jContentPane.add(targetBeanObject, 0); //add at index 0
  220.             //set the background color the target bean object to say pink
  221.             //validate the contentpane.
  222.             jContentPane.validate();
  223.         }
  224.  
  225.         //Create the Class object for the target bean class using bean’s class name.
  226.         //Create BeanInfo object using the Class object.
  227.         BeanInfo bi = null;
  228.        
  229.         try {
  230.             classObject = Class.forName(className);
  231.  
  232.  
  233.             //The first parameter below indicates the class object to be used for getting properties.
  234.             //The second parameter indicates the parent Class object before which to stop getting properties.
  235.             //Below, getBeanInfo would get properties from classObject class object but won’t get
  236.             //properties from JPanel class object which is its parent i.e. it would stop getting properties
  237.             // at JPanel class object.
  238.             //If the second parameter below was Object.class, it would get properties from classObject
  239.             // class object and also from JPanel class object (the parent), but won’t get properties
  240.             // from Object class object (the grand parent) i.e. it would stop at Object class object.
  241.             bi = Introspector.getBeanInfo(classObject, JPanel.class);
  242.         }
  243.  
  244.         catch (ClassNotFoundException ex) {
  245.         }
  246.        
  247.         catch (IntrospectionException ex) {
  248.         }
  249.  
  250.         //Get an array of PropertyDescriptor objects from BeanInfo object.
  251.         pd = bi.getPropertyDescriptors();
  252.         String propName ;
  253.  
  254.         for (int i=0; i< pd.length; i++) {
  255.             //Get property name from corresponding PropertyDescriptor array element.
  256.             //Set property name in the corresponding JLabel array element.
  257.             propName = pd[i].getName();
  258.             //Set propName as the text for the corresponding JLable.
  259.             Method mget = pd[i].getReadMethod();
  260.             Object robj = null;
  261.             try {
  262.                 robj = mget.invoke(targetBeanObject, null);
  263.             }
  264.            
  265.             catch (IllegalAccessException ex) {
  266.             }
  267.            
  268.             catch (IllegalArgumentException ex) {
  269.             }
  270.            
  271.             catch (InvocationTargetException ex) {
  272.             }
  273.  
  274.             //convert the received object contents to a String
  275.             String sobj = robj.toString();
  276.            
  277.             //Set the String sobj as the text in the corresponding text field.
  278.             jtfPropValues[i].setText(sobj);
  279.             jlbPropNames[i].setText(propName);
  280.            
  281.       }
  282.     }
  283.     /* drop down event end */
  284.  
  285.     /** text field actionListener event begin*/
  286.     public void jtfActionPerformed(ActionEvent e) {
  287.         int i;
  288.         String propName="", propValue="";
  289.  
  290.         //Determine the name, value and index of the property that changed.
  291.         for (i=0; i<jtfPropValues.length;i++) {
  292.             if (e.getSource()== jtfPropValues[i]) {
  293.                 break;
  294.             }
  295.         }
  296.  
  297.         //Get the property name and the property value from the
  298.         //JLabel and JTextfield corresponding to the i value.
  299.         propName = jlbPropNames[i].getName();
  300.         propValue = jtfPropValues[i].getText();
  301.  
  302.         //Note that the property index for the above property in the pd
  303.         //array is the same. So you can go to that index in pd array and
  304.         //access its property type .
  305.         Class propType = pd[i].getPropertyType();
  306.  
  307.         //Get the property type as a String
  308.         String propTypeName = propType.getName();
  309.  
  310.  
  311.         //Create Object array for storing parameters
  312.         Object[] params = new Object[1];
  313.  
  314.         //Depending upon property name, create correct parameter object.
  315.         if (propTypeName.equals("int")) {
  316.             params [0] = new Integer (Integer.parseInt(propValue));
  317.         }
  318.        
  319.         else if (propTypeName.equals("double")) {
  320.             params [0] = new Double (Double.parseDouble(propValue));
  321.         }
  322.        
  323.         else if (propTypeName.equals("boolean")) {
  324.             params [0] = new Boolean(propValue);
  325.         }
  326.        
  327.         else if (propTypeName.equals("java.lang.String")) {
  328.             params [0] = propValue;
  329.         }
  330.  
  331.         //Get the set method object.
  332.         Method mset = pd[i].getWriteMethod();
  333.  
  334.         //Invoke set method and pass it target bean and parameters.
  335.         try {
  336.             mset.invoke(targetBeanObject, params);
  337.         }
  338.        
  339.         catch (IllegalAccessException ex) {
  340.         }
  341.  
  342.         catch (IllegalArgumentException ex) {
  343.         }
  344.        
  345.         catch (InvocationTargetException ex) {
  346.         }
  347.  
  348.     }
  349.     /* actionListener event end */
  350.  
  351.     /* this method initializes this */
  352.     private void initialize() {
  353.         this.setSize(300, 200);
  354.         this.setContentPane(getJContentPane());
  355.         this.setTitle("JFrame");
  356.     }
  357.  
  358.     /* This method initializes jContentPane */
  359.     private JPanel getJContentPane() {
  360.         if (jContentPane == null) {
  361.             GridLayout gridLayout = new GridLayout();
  362.             gridLayout.setRows(1);
  363.             gridLayout.setColumns(2);
  364.             jContentPane = new JPanel();
  365.             jContentPane.setLayout(gridLayout);
  366.             jContentPane.add(getJpLeft(), null);
  367.             jContentPane.add(getJpRight(), null);
  368.         }
  369.         return jContentPane;
  370.     }
  371.  
  372.     public void actionPerformed(ActionEvent e) {
  373.         // TODO Auto-generated method stub
  374.        
  375.     }
  376.  
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement