The Artima Developer Community
Sponsored Link

Browse User Interfaces for Jini Services
Build a JFC-Based Application for Browsing and Launching Jini Services
by Jeffrey Peden
First Published in JavaWorld, March 2001

<<  Page 3 of 7  >>


TimeService Implementation

Implementing the TimeService interface is equally as simple:

public class TimeServiceImpl extends java.lang.Object
             implements TimeService{

   public TimeServiceImpl() {
      //Nothing to do here
   public java.util.Date getTime() {
      return new Date();


Since that object needs no resources, there is nothing to do in a constructor. Our single method inherited from the TimeService interface instantiates and returns a new Date object, which is initialized by default to the current system time.

Now we'll get into the code for the user interface. While I will typically define more complex user interfaces as distinct classes, I've chosen to implement the UI as a static inner class.

The code below declares a new class, TimeJFrame, which extends javax.swing.JFrame and implements java.awt.event.ActionListener. The constructor takes a net.jini.core.lookup.ServiceItem as the sole parameter. From the ServiceItem, we extract the service proxy and store it as an instance variable. The constructor also creates two Swing components, a JButton and a JLabel, and adds them to its content pane. The class registers itself to receive events from the button, so that we may respond and update the UI with the current time from the service:

   protected static class TimeJFrame extends JFrame implements ActionListener{
      protected TimeService jini_Time_Service;
      protected JLabel      comp_Time_Label;
      protected JButton     comp_Time_Button;
      protected SimpleDateFormat util_Date_Format;
      public TimeJFrame(ServiceItem item){
         super("Time Service UI");
         util_Date_Format = new SimpleDateFormat();
         comp_Time_Button = new JButton("Update Time");
         comp_Time_Label = new JLabel();
         comp_Time_Label.setBorder( BorderFactory.createTitledBorder(
            BorderFactory.createEtchedBorder(),"Time is:"));
         getContentPane().setLayout(new BorderLayout());
      public void actionPerformed(ActionEvent evt){
            Date d = jini_Time_Service.getTime();
         }catch(RemoteException e){

The view of a TimeJFrame when launched
from the JiniTaskBar

The actionPerformed() method responds to button clicks by retrieving the current time from the service and updating the UI with a formatted text string representing the time. Since we declared the getTime() method in TimeService to throw an exception, we have enclosed the code within a try-catch block. We should take action to tell the user that an exception has occurred, but for now we will simply print out the stack trace. Since our implementation is not a remote object, the exception should never be thrown, but we need to take caution in case the implementation changes.

<<  Page 3 of 7  >>

Sponsored Links

Copyright © 1996-2017 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us