package intro; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.BeanInfo; import java.beans.Beans; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import javax.swing.JTextField; public class JFrameExt extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JPanel jpLeft = null; private JPanel jpRight = null; private JPanel jpController = null; private JPanel jpInspector = null; private JComboBox jcboMenu = null; private JPanel jpPropNames = null; private JPanel jpPropValues = null; //Create an array of 10 JLabels private JLabel[] jlbPropNames = new JLabel[10]; //Create an array of 10 JTextFields private JTextField[] jtfPropValues = new JTextField[10]; private JPanel targetBeanObject = null; private Class classObject = null; private PropertyDescriptor[] pd = null; /** * This method initializes jpLeft * * @return javax.swing.JPanel */ private JPanel getJpLeft() { if (jpLeft == null) { jpLeft = new JPanel(); jpLeft.setLayout(new GridBagLayout()); } return jpLeft; } /** * This method initializes jpRight * * @return javax.swing.JPanel */ private JPanel getJpRight() { if (jpRight == null) { jpRight = new JPanel(); jpRight.setLayout(new BorderLayout()); jpRight.add(getJpController(), BorderLayout.NORTH); jpRight.add(getJpInspector(), BorderLayout.CENTER); } return jpRight; } /** * This method initializes jpController * * @return javax.swing.JPanel */ private JPanel getJpController() { if (jpController == null) { GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.fill = GridBagConstraints.VERTICAL; gridBagConstraints.weightx = 1.0; jpController = new JPanel(); jpController.setLayout(new GridBagLayout()); jpController.add(getJcboMenu(), gridBagConstraints); } return jpController; } /** * This method initializes jpInspector * * @return javax.swing.JPanel */ private JPanel getJpInspector() { if (jpInspector == null) { GridLayout gridLayout1 = new GridLayout(); gridLayout1.setRows(1); gridLayout1.setColumns(2); jpInspector = new JPanel(); jpInspector.setLayout(gridLayout1); jpInspector.add(getJpPropNames(), null); jpInspector.add(getJpPropValues(), null); } return jpInspector; } /* method initializing jcboMenu combo box */ private JComboBox getJcboMenu() { if (jcboMenu == null) { jcboMenu = new JComboBox(); jcboMenu.setEditable (false); jcboMenu.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { // draw the selected item jcboClassName_actionPerformed(e); } }); jcboMenu.addItem(""); jcboMenu.addItem("intro.Rect"); jcboMenu.addItem("intro.Circ"); jcboMenu.addItem("intro.Ticker"); } return jcboMenu; } /** * This method initializes jpPropNames * * @return javax.swing.JPanel */ private JPanel getJpPropNames() { if (jpPropNames == null) { GridLayout gridLayout2 = new GridLayout(); gridLayout2.setRows(10); gridLayout2.setColumns(1); jpPropNames = new JPanel(); jpPropNames.setLayout(gridLayout2); } return jpPropNames; } /** * This method initializes jpPropValues * * @return javax.swing.JPanel */ private JPanel getJpPropValues() { if (jpPropValues == null) { GridLayout gridLayout3 = new GridLayout(); gridLayout3.setRows(10); gridLayout3.setColumns(1); jpPropValues = new JPanel(); jpPropValues.setLayout(gridLayout3); } return jpPropValues; } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrameExt thisClass = new JFrameExt(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.setVisible(true); } }); } /** * This is the default constructor */ public JFrameExt() { super(); initialize(); //Creating JLabel objects one by one in a loop for (int j = 0; j < 10; j++) { //Create JLabel object jlbPropNames[j] = new JLabel(""); //Add JLabel object to JPanel jpPropNames.add(jlbPropNames[j]); } //Creating JTextField objects one by one in a loop for (int j = 0; j < 10; j++) { //Create JTextField objects. Each of width 10 jtfPropValues[j] = new JTextField(); //Register this as the listener with JTextField object jtfPropValues[j].addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { // reflect changes jtfActionPerformed(e); } }); //Add JTextField object to JPanel jpPropValues.add(jtfPropValues[j]); } } /** drop down event begin*/ void jcboClassName_actionPerformed(ActionEvent e) { JPanel targetBeanObject = null; //Get target bean class name from combo box String className = (String)jcboMenu.getSelectedItem(); if (className.equals("")) return; //Create target bean object using bean class name. try { targetBeanObject = (JPanel)Beans.instantiate(null, className); } catch (ClassNotFoundException ex) { } catch (IOException ex) { } //If targetBeanObject is a JPanel, //remove jpLeft and replace it with targetBeanObject //and validate (refresh) the content pane. if (targetBeanObject instanceof JPanel) { jContentPane.remove(0); //remove component at index 0 jContentPane.add(targetBeanObject, 0); //add at index 0 //set the background color the target bean object to say pink //validate the contentpane. jContentPane.validate(); } //Create the Class object for the target bean class using bean’s class name. //Create BeanInfo object using the Class object. BeanInfo bi = null; try { classObject = Class.forName(className); //The first parameter below indicates the class object to be used for getting properties. //The second parameter indicates the parent Class object before which to stop getting properties. //Below, getBeanInfo would get properties from classObject class object but won’t get //properties from JPanel class object which is its parent i.e. it would stop getting properties // at JPanel class object. //If the second parameter below was Object.class, it would get properties from classObject // class object and also from JPanel class object (the parent), but won’t get properties // from Object class object (the grand parent) i.e. it would stop at Object class object. bi = Introspector.getBeanInfo(classObject, JPanel.class); } catch (ClassNotFoundException ex) { } catch (IntrospectionException ex) { } //Get an array of PropertyDescriptor objects from BeanInfo object. pd = bi.getPropertyDescriptors(); String propName ; for (int i=0; i< pd.length; i++) { //Get property name from corresponding PropertyDescriptor array element. //Set property name in the corresponding JLabel array element. propName = pd[i].getName(); //Set propName as the text for the corresponding JLable. Method mget = pd[i].getReadMethod(); Object robj = null; try { robj = mget.invoke(targetBeanObject, null); } catch (IllegalAccessException ex) { } catch (IllegalArgumentException ex) { } catch (InvocationTargetException ex) { } //convert the received object contents to a String String sobj = robj.toString(); //Set the String sobj as the text in the corresponding text field. jtfPropValues[i].setText(sobj); jlbPropNames[i].setText(propName); } } /* drop down event end */ /** text field actionListener event begin*/ public void jtfActionPerformed(ActionEvent e) { int i; String propName="", propValue=""; //Determine the name, value and index of the property that changed. for (i=0; i