HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"BeanShell & DynamicJava: Java Scripting with Java"
Vol. 5, Issue 7, p. 94

	

Listing 1: BeanShell Employee "class": 

Employee (){ 
String firstName, lastName; 
int id, dept; 
manager = null; 
__init(){ 
print("This far"); 
super.firstName = "John"; 
super.lastName = "Doe"; 
super.id = 1; 
super.manager=null; 
super.dept=1; 
} 
init(String fname, String lname, int id, manager, int dept){ 
super.firstName = fname; 
super.lastName = lname; 
super.id = id; 
super.manager = manager; 
super.dept = dept; 
} 
getManager(){ 
return this.manager; 
} 
String toString(){ 
StringBuffer buf = new StringBuffer(); 
buf.append(super.lastName+','); 
buf.append(super.firstName+','); 
buf.append(""+super.id); 
return buf.toString(); 
} 
__init(); 
return this; 
} 
print(Employee().toString()); 
joe = Employee(); joe.init("Joe", "Batista", 100, null, 1); 
ron = Employee(); ron.init("Ron", "Furgeson", 101, joe, 1); 
print(ron.toString()); 
print(ron.getManager().toString()); 
 
Listing 2: Java Employee class 

public class Employee{ 
private String firstName, lastName; 
private int id, dept; 
private Employee manager; 
public Employee(){ 
firstName = "John"; 
lastName = "Doe"; 
id = 1; 
manager=null; 
dept=1; 
} 
public Employee(String fname, String lname, int id, Employee 
manager, int dept){ 
firstName = fname; 
lastName = lname; 
this.id = id; 
this.manager = manager; 
this.dept = dept; 
} 
public Employee getManager(){ 
return manager; 
} 
public String toString(){ 
StringBuffer buf = new StringBuffer(); 
buf.append(lastName+','); 
buf.append(firstName+','); 
buf.append(""+id); 
return buf.toString(); 
} 
... 
... 
} 
 
Listing 3: BeanShell EmployeeForm: 

import javax.swing.*; 
import java.awt.GridBagLayout; 
import java.awt.GridBagConstraints; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import employee.Employee; 
EmployeeForm(){ 
JFrame frame; 
JTextField name; 
JTextField id; 
void init(){ 
super.frame = new JFrame("Employee Form"); 
pane = new JPanel(); 
super.frame.getContentPane().add(pane); 
pane.setLayout(new GridBagLayout()); 
// Create a name, and id text field. 
super.name = new JTextField(25); 
super.id = new JTextField(10); 
// Create and add a "Name" and "ID" label. 
JLabel nameLabel = new JLabel("Name"); 
nameLabel.setLabelFor(name); 
nameLabel.setDisplayedMnemonic('N'); 
GridBagConstraints constraint = new GridBagConstraints(); 
pane.add(nameLabel, constraint); 
JLabel idLabel = new JLabel("ID"); 
idLabel.setLabelFor(id); 
idLabel.setDisplayedMnemonic('I'); 
constraint.gridy=1; 
pane.add(idLabel, constraint); 
// Add the name and ID text field to the form. 
constraint.gridy=0; constraint.gridx=1; 
constraint.weightx=80.00; 
constraint.anchor=GridBagConstraints.WEST; 
pane.add(name, constraint); 
constraint.gridy=1; 
pane.add(id, constraint); 
// Create an okay button, add it, and set up its event handler. 
JButton okay = new JButton("Okay"); 
okay.setMnemonic('O'); 
constraint.gridx=1; constraint.gridy=2; 
constraint.anchor=GridBagConstraints.EAST; 
pane.add(okay, constraint); 
okay.addActionListener(this); 
frame.setVisible(true); 
frame.pack(); 
} 
void actionPerformed(ActionEvent event){ 
handleOkay(); 
} 
void handleOkay(){ 
String name, fname, lname; 
int index=0; 
int id =0; 
name = super.name.getText(); 
index = name.indexOf(" "); 
fname = name.substring(0, index); 
lname = name.substring(index+1, name.length()); 
id = Integer.parseInt(super.id.getText()); 
Employee employee = new Employee(fname, lname, id, null, 100); 
System.out.println(""+employee); 
} 
init(); 
return this; 
} 
ef = EmployeeForm(); 
 
Listing 4: Java EmployeeForm: 

import javax.swing.*; 
import java.awt.GridBagLayout; 
import java.awt.GridBagConstraints; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import employee.Employee; 
public class EmployeeForm extends JFrame{ 
private JTextField name; 
private JTextField id; 
public EmployeeForm(){ 
super("Employee Form"); 
JPanel pane = new JPanel(); 
getContentPane().add(pane); 
pane.setLayout(new GridBagLayout()); 
// Create a name, and id text field. 
name = new JTextField(25); 
id = new JTextField(10); 
// Create and add a "Name" and "ID" label. 
JLabel nameLabel = new JLabel("Name"); 
nameLabel.setLabelFor(name); 
nameLabel.setDisplayedMnemonic('N'); 
GridBagConstraints constraint = new GridBagConstraints(); 
pane.add(nameLabel, constraint); 
JLabel idLabel = new JLabel("ID"); 
idLabel.setLabelFor(id); 
idLabel.setDisplayedMnemonic('I'); 
constraint.gridy=1; 
pane.add(idLabel, constraint); 
// Add the name and ID text field to the form. 
constraint.gridy=0; constraint.gridx=1; 
constraint.weightx=80.00; 
constraint.anchor=GridBagConstraints.WEST; 
pane.add(name, constraint); 
constraint.gridy=1; 
pane.add(id, constraint); 
// Create an okay button, add it, and set up its event handler. 
JButton okay = new JButton("Okay"); 
okay.setMnemonic('O'); 
constraint.gridx=1; constraint.gridy=2; 
constraint.anchor=GridBagConstraints.EAST; 
pane.add(okay, constraint); 
okay.addActionListener(new ActionListener(){ 
public void actionPerformed(ActionEvent event){ 
handleOkay(); 
} 
}); 
this.setVisible(true); 
this.pack(); 
} 
public void handleOkay(){ 
String name, fname, lname; 
int index=0; 
int id =0; 
name = this.name.getText(); 
index = name.indexOf(" "); 
fname = name.substring(0, index); 
lname = name.substring(index+1, name.length()); 
id = Integer.parseInt(this.id.getText()); 
Employee employee = new Employee(fname, lname, id, null, 100); 
System.out.println(""+employee); 
} 
public static void main(String [] args){ 
new EmployeeForm(); 
} 
} 
  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.