Client ImplementationListing 9.4 CustomClient.java (Complete Client code)import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.rmi.*; import java.util.*; //client implementation public class CustomClient extends JFrame{ //instance variables private JLabel lblName,lblAge,lblItems; private JTextField txtName,txtAge; private JTextArea txtMessage; private JComboBox cboItems; private JButton btnSubmit; private CustomInterface server; //constructor public CustomClient(){ super("Client Window"); Container cp = getContentPane(); JPanel topPanel = new JPanel(new GridLayout(3,2)), centerPanel = new JPanel(new BorderLayout()), btnPanel = new JPanel(); lblName = new JLabel("Name"); lblAge = new JLabel("Age"); lblItems = new JLabel("Items"); //text field for entering customer name. txtName = new JTextField(15); //text field for entering customer age. txtAge = new JTextField(5); //text area to show message txtMessage = new JTextArea(5,20); //button to submit. btnSubmit = new JButton("Sumbit"); btnSubmit.addActionListener(new ButtonHandler()); try{ //get a remote object server = (CustomInterface)Naming.lookup("Server"); //get all the products sold by the company. Vector list = server.getList(); cboItems = new JComboBox(list); }catch(Exception ex){ txtMessage.setText(ex.getMessage()); } //add components to containers. topPanel.add(lblName); topPanel.add(txtName); topPanel.add(lblAge); topPanel.add(txtAge); topPanel.add(lblItems); topPanel.add(cboItems); centerPanel.add(txtMessage); btnPanel.add(btnSubmit); cp.add(topPanel,BorderLayout.NORTH); cp.add(centerPanel); cp.add(btnPanel,BorderLayout.SOUTH); //set the default close operation this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //pack() causes the window to be sized to fit the //preferred size and layouts of its subcomponents. pack(); //set the location of window setLocation(15,15); //Make the Window visible. show(); } //main method public static void main(String args[]){ new CustomClient(); } //event handling class private class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent ae){ //get the name of customer String name = txtName.getText(); //get the age of customer int age = Integer.parseInt(txtAge.getText()); //get the selected product String item = (String)cboItems.getSelectedItem(); //create a new customer object Customer cm = new Customer(name,age,item); try{ //call remote method String msg = server.submit(cm); //shows the message returned by the server txtMessage.setText(msg); }catch(Exception ex){ txtMessage.setText(ex.getMessage()); } } } } ExplanationThe client creates a number of components and containers. The combo box is filled with a Vector returned by the remote getList() method. When a user clicks the submit button, a new Customer objects is created and passed to the submit() method of the remote object. When a non-remote object is passed to another machine, the JVM makes a copy and sends that copy across the network (see Figure 9.1).
|
||
|
||
Copyright © 2001 www.universalteacher.com |
||