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<jtfPropValues.length;i++) {
if (e.getSource()== jtfPropValues[i]) {
break;
}
}
//Get the property name and the property value from the
//JLabel and JTextfield corresponding to the i value.
propName = jlbPropNames[i].getName();
propValue = jtfPropValues[i].getText();
//Note that the property index for the above property in the pd
//array is the same. So you can go to that index in pd array and
//access its property type .
Class propType = pd[i].getPropertyType();
//Get the property type as a String
String propTypeName = propType.getName();
//Create Object array for storing parameters
Object[] params = new Object[1];
//Depending upon property name, create correct parameter object.
if (propTypeName.equals("int")) {
params [0] = new Integer (Integer.parseInt(propValue));
}
else if (propTypeName.equals("double")) {
params [0] = new Double (Double.parseDouble(propValue));
}
else if (propTypeName.equals("boolean")) {
params [0] = new Boolean(propValue);
}
else if (propTypeName.equals("java.lang.String")) {
params [0] = propValue;
}
//Get the set method object.
Method mset = pd[i].getWriteMethod();
//Invoke set method and pass it target bean and parameters.
try {
mset.invoke(targetBeanObject, params);
}
catch (IllegalAccessException ex) {
}
catch (IllegalArgumentException ex) {
}
catch (InvocationTargetException ex) {
}
}
/* actionListener event end */
/* this method initializes this */
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/* This method initializes jContentPane */
private JPanel getJContentPane() {
if (jContentPane == null) {
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(1);
gridLayout.setColumns(2);
jContentPane = new JPanel();
jContentPane.setLayout(gridLayout);
jContentPane.add(getJpLeft(), null);
jContentPane.add(getJpRight(), null);
}
return jContentPane;
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}