class ClientUI extends JFrame
{
JTextArea fieldOutput;
JTextField fieldInput;
JButton buttonSend;
JLabel labelInput, labelOutput;
ClientUI()
{
super("My Messenger");
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.setBackground(Color.BLACK);
labelOutput = new JLabel ("Your friends says :");
fieldOutput = new JTextArea(15,30);
fieldOutput.setForeground(Color.BLUE);
fieldOutput.setBackground(Color.GRAY);
fieldOutput .setFont(new Font("SansSerif" , Font.BOLD, 16));
fieldOutput.setEditable(false);
labelInput = new JLabel ("Enter text that you want to chat");
fieldInput = new JTextField (30);
fieldInput .setForeground(Color.BLUE);
fieldInput .setFont(new Font("SansSerif" , Font.BOLD, 15));
fieldInput .setToolTipText("Enter the text that you want to send to
your friend");
fieldInput.setBackground(Color.LIGHT_GRAY);
buttonSend = new JButton("Send Message" );
c.add(labelOutput );
c.add(fieldOutput );
c.add(labelInput );
c.add(fieldInput );
c.add(buttonSend );
ClientUIHandler ch = new ClientUIHandler ();
buttonSend .addActionListener(ch);
fieldInput.addActionListener(ch);
} //end of constructor
private class ClientUIHandler implements ActionListener
{
String sOutput="";
public void actionPerformed(ActionEvent e)
{
try
{
InetAddress ipAddress = InetAddress.getLocalHost();
String sIP = ipAddress .getHostAddress() ;
hello server = (hello)Naming .
lookup("rmi://192.168.2.25/HelloServer");
String sInput = fieldInput.getText();
fieldInput.setText("");
String tempStr = server.DisplayHello(sIP+":"+sInput);
sOutput = sOutput + tempStr +"\n";
fieldOutput.setText(sOutput);
} //end of try block
catch (Exception ex)
{
System.out.println(ex);
}
}
} //end of private action listener class
} // end of outer class
public class Client
{
public static void main(String arg[])
{
ClientUI cui = new ClientUI ();
cui.setSize(420,510);
cui.setVisible(true);
cui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
/***************************************************************
*******************/
//hello.java
import java.rmi.*;
public interface hello extends Remote
{
public String DisplayHello(String s)throws RemoteException;
}
/***************************************************
*********/
//HelloServerImpl.java
import javax.swing.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class HelloServerImpl extends UnicastRemoteObject implements
hello
{
public HelloServerImpl() throws RemoteException
{
super();
}
public String DisplayHello(String sFrmClient) throws RemoteException
{
System.out.println( sFrmClient);
String sinput = JOptionPane.showInputDialog("Enter some chat text ");
return sinput;
}
public static void main(String argc[])
{
System.setSecurityManager(new RMISecurityManager());
try
{
HelloServerImpl instance = new HelloServerImpl();
Naming.rebind("HelloServer",instance);
System.out.println("Server Registered -All the best");
}
catch (Exception e)
{
System.err.println(e);
}
}
}
rmic HelloServerImpl
start rmiregistry
finally run the programs
ALSO DONT FORGET TO CHANGE THE IP ADDRESS OF THE SERVER IN CLIENT.JAVA
short term
=========
+ Add scrollpane to the output text field
+ convert ip address to valid names
+ Ability to run confereneces, all the people who participate in the
chat must be able to see each other's messages
long term
=========
+ implement JDBC
+ apply user name and passwords