Client Implementation

Listing 10.3 SingleThreadClient

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.rmi.*;

//client implementation
public class SingleThreadClient 
	extends JFrame{
	
   //instance variables
   private JTextArea txtMessage;
   private JButton btnStart,btnExit;
   private SingleThreadInterface remote;
	
   //constructor
   public SingleThreadClient(){
 	 super("Single Thread");
		
	//get a content pane object
	Container cp = getContentPane();
		
	JPanel btnPanel = new JPanel();

	//text area used to show message
	txtMessage = new JTextArea(8,25);

	//button used to call the remote method
	btnStart = new JButton("Start");

	//button used to exit application.
	btnExit = new JButton("Exit");

	//add listener to buttons
	ButtonHandler handler = new ButtonHandler();
	btnStart.addActionListener(handler);
	btnExit.addActionListener(handler);

	//add components to containers
	btnPanel.add(btnStart);btnPanel.add(btnExit);
	cp.add(txtMessage);
	cp.add(btnPanel,BorderLayout.SOUTH);
	try{
	   //get a remote reference
	   remote = (SingleThreadInterface)Naming.lookup("Server");
	}catch(Exception ex){
	  txtMessage.setText(ex.getMessage());
        }
	
	//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();

	//Make the Window visible.
	show();
   }

    //main method 	
    public static void main(String args[]){
 	new SingleThreadClient();
    }
	
    //event handling class
     private class ButtonHandler 
	implements ActionListener{
	public void actionPerformed(ActionEvent ae){
		 
	 //get the source
	 JButton temp = (JButton)ae.getSource();
		 
	 if(temp == btnStart){
	   try{
		 txtMessage.setText("Calling remote method");
	 	 String msg = remote.dangerousLoop();
	 	 txtMessage.setText(msg);
	     }catch(Exception ex){
		txtMessage.setText(ex.getMessage());
	     }
	    }
	   else{
		System.exit(0);
	       }
	   }
      }
}
 

 

RMI BOOK MAIN PAGE                Top

  
Copyright © 2001 www.universalteacher.com