Tue Feb 03, 2009 7:17 pm
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.rmi.*;
import java.util.*;
import java.rmi.server.*;
public class ChatClient extends JFrame implements ChatClientInterface,ActionListener
{
CenterPanel centerPanel;
EastPanel eastPanel;
SouthPanel southPanel;
Container contentPane;
String clientName;
//String refIdentity="rmi://localhost:1099/Anoop";
String refIdentity;
ChatServerInterface serverRef;
Hashtable hs;
public ChatClient(final String clientName)
{
super("Chat Client "+clientName);
this.clientName=clientName;
refIdentity="serverstub";
hs=new Hashtable();
contentPane=getContentPane();
centerPanel=new CenterPanel();
eastPanel=new EastPanel();
southPanel=new SouthPanel();
contentPane.add(centerPanel,BorderLayout.CENTER);
contentPane.add(eastPanel,BorderLayout.EAST);
contentPane.add(southPanel,BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
southPanel.sendBtn.addActionListener(this);
southPanel.textField.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
try
{
serverRef.logout(clientName);
}
catch (Exception exception)
{
System.out.println(exception);
}
}
});
eastPanel.list.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
if (evt.getClickCount()==2)
{
String hisName=(String)eastPanel.list.getSelectedValue();
MiniWindow mw=(MiniWindow)hs.get(hisName);
if (mw==null)
{
try
{
ChatClientInterface hisRef=serverRef.giveRef(hisName);
mw=new MiniWindow(hisName,hisRef);
hs.put(hisName,mw);
}
catch (Exception e)
{
System.out.println(e);
}
}
eastPanel.list.clearSelection();
southPanel.textField.requestFocus();
}
}
});
pack();
setVisible(true);
setResizable(false);
try
{
//ChatServerInterface
serverRef=(ChatServerInterface)Naming.lookup(refIdentity);
UnicastRemoteObject.exportObject(this);
serverRef.login(clientName,this);
}
catch (Exception e)
{
System.out.println(e);
}
}
public void pm(final String hisName,final ChatClientInterface hisRef,final String hisMsg)
{
MiniWindow mw=(MiniWindow)hs.get(hisName);
if (mw==null)
{
mw=new MiniWindow(hisName,hisRef);
hs.put(hisName,mw);
}
mw.p1.ta.append(hisMsg+"\n");
}
public void actionPerformed(ActionEvent e)
{
String clientMsg=southPanel.textField.getText();
clientMsg=clientName+" : "+clientMsg;
try
{
serverRef.takeMsg(clientMsg);
}
catch (Exception exception)
{
System.out.println(exception);
}
southPanel.textField.setText("");
}
public void takeMsg(String serverMsg)
{
centerPanel.textArea.append(serverMsg+"\n");
}
public void takeClientList(Vector clientNames)
{
eastPanel.listModel.removeAllElements();
Enumeration en=clientNames.elements();
while (en.hasMoreElements())
{
String clientName=(String)en.nextElement();
eastPanel.listModel.addElement(clientName);
}
}
class MiniWindow extends JFrame implements ActionListener
{
Panel1 p1;
Panel2 p2;
String hisName;
ChatClientInterface hisRef;
public MiniWindow(final String hisName,final ChatClientInterface hisRef)
{
super("From "+clientName+ " to "+hisName);
this.hisName=hisName;
this.hisRef=hisRef;
p1=new Panel1();
p2=new Panel2();
add(p1,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
hs.remove(hisName);
}
});
p2.t.addActionListener(this);
p2.b.addActionListener(this);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setVisible(true);
setResizable(false);
p2.t.requestFocus();
}
public void actionPerformed(ActionEvent evt)
{
String msg=p2.t.getText();
msg=clientName + ":" +msg;
try
{
hisRef.pm(clientName,ChatClient.this,msg);
}
catch (Exception e)
{
System.out.println(e);
}
p1.ta.append(msg+"\n");
p2.t.setText("");
}
class Panel1 extends JPanel
{
JTextArea ta;
JScrollPane sp;
public Panel1()
{
ta=new JTextArea(10,20);
add(sp=new JScrollPane(ta));
ta.setEditable(false);
ta.setBackground(Color.lightGray);
Border raisedbevel=BorderFactory.createRaisedBevelBorder();
TitledBorder title=BorderFactory.createTitledBorder(raisedbevel,"View Massages");
title.setTitleJustification(TitledBorder.CENTER);
ta.setBorder(title);
}
};
class Panel2 extends JPanel
{
JTextField t;
JButton b;
public Panel2()
{
add(t=new JTextField(10));
Border backline=BorderFactory.createLineBorder(Color.black);
TitledBorder title=BorderFactory.createTitledBorder(backline,"Enter Massage");
title.setTitleJustification(TitledBorder.CENTER);
t.setBorder(title);
add(b=new JButton("SEND"));
}
};
};
public static void main(String args[])
{
try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e)
{
System.out.println(e);
}
new ChatClient(args[0]);
}
};
class CenterPanel extends JPanel
{
JTextArea textArea;
JScrollPane scrollpane;
public CenterPanel()
{
textArea=new JTextArea(20,35);
add(scrollpane=new JScrollPane(textArea));
textArea.setEditable(false);
textArea.setOpaque(true);
textArea.setBackground(Color.lightGray);
Border raisedbevel=BorderFactory.createRaisedBevelBorder();
TitledBorder title=BorderFactory.createTitledBorder(raisedbevel,"View Massages");
title.setTitleJustification(TitledBorder.CENTER);
textArea.setBorder(title);
}
};
class EastPanel extends JPanel
{
JList list;
JScrollPane scrollpane;
DefaultListModel listModel;
public EastPanel()
{
listModel=new DefaultListModel();
list=new JList(listModel);
add(scrollpane= new JScrollPane(list));
list.setVisibleRowCount(20);
list.setBackground(Color.lightGray);
Border raisedbevel=BorderFactory.createRaisedBevelBorder();
TitledBorder title=BorderFactory.createTitledBorder(raisedbevel,"View Clients");
title.setTitleJustification(TitledBorder.CENTER);
list.setBorder(title);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
}
};
class SouthPanel extends JPanel
{
JTextField textField;
JButton sendBtn;
public SouthPanel()
{
add(textField=new JTextField(30));
Border blackline=BorderFactory.createLineBorder(Color.black);
TitledBorder title=BorderFactory.createTitledBorder(blackline,"Enter Message");
title.setTitleJustification(TitledBorder.CENTER);
textField.setBorder(title);
add(sendBtn=new JButton("Send Message"));
setSize(40,100);
}
};
Wed Feb 04, 2009 12:18 am
Sun Jan 20, 2013 10:53 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.