Chapter 4 Exporting an Object | ||
Explanation of CodeImporting necessary packages. import javax.swing.*; import java.rmi.*; import java.awt.*; import java.awt.event.*; The GreaterClient class declares a number instance variables. Here is the GreaterClient class declaration: public class GreaterClient extends JFrame{ //instance variables private JTextField txtOne,txtTwo; private JTextArea txtMessage; private JLabel lblOne,lblTwo; private JButton btnCompare; private GreaterInterface obj; The createComponents() methods is called by the constructor. The createComponents() method creates a number of components and containers. It creates two TextField instances for entering two numbers, a TextArea to show the value returned by the remote method. Moreover, it creates an instance of button to call the remote method and adds an event listener to the button. //method to create various components private void createComponents(){ Container cp = getContentPane(); //panels for buttons,textfields. JPanel topPanel = new JPanel(new GridLayout(2,2)), centerPanel= new JPanel(new BorderLayout()), btnPanel = new JPanel(new GridLayout(1,1)); //text fields for entering numbers txtOne = new JTextField(5); txtTwo = new JTextField(5); lblOne = new JLabel("First Number"); lblTwo = new JLabel("Second Number"); //text area used to show message txtMessage = new JTextArea(7,25); //button used to call remote method btnCompare = new JButton("Compare"); //Add an ActionListener to the button. btnCompare.addActionListener(new ButtonHandler()); //add the components topPanel.add(lblOne);topPanel.add(txtOne); topPanel.add(lblTwo);topPanel.add(txtTwo); centerPanel.add(txtMessage); btnPanel.add(btnCompare); cp.add(topPanel,BorderLayout.NORTH); cp.add(centerPanel); cp.add(btnPanel,BorderLayout.SOUTH); }
The connect() method is used to get a remote reference. //method used to connect to the server private void connect(){ try{ obj = (GreaterInterface)Naming.lookup("Server"); }catch(Exception ex){ txtMessage.setText(ex.getMessage()); } }
The main method just creates an instance of the GreaterClient class. //main method public static void main(String args[]){ new GreaterClient(); }
Event Handling Class (ButtonHandler)This class is an inner class used to handle action events generated by buttons. When a user presses the button, the actionPerformed() method is invoked. The actionPerformed() method calls the getGreater() method of remote object. The value returned by the getGreater() method will be displayed in the TextArea. //Event handling class private class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent ae){ try{ //get the value of first text field int one = Integer.parseInt(txtOne.getText()); //get the value of second text field int two = Integer.parseInt(txtTwo.getText()); //calling the remote method String msg = obj.getGreater(one,two); //shows the value returned by the method txtMessage.setText(msg); }catch(Exception ex){ txtMessage.setText("Error: " +ex.getMessage()); } } } |
||
|
||
Copyright © 2001 www.universalteacher.com |
||