Chat -Client Server Programming

INTRODUCTION

Let Us Start making up our own chat

The steps are as follows

1.Install JDK AND JRE 5.o or upper version

2.I have used JDK 6_0_21 update

3.Install My Eclipse 7.5

4.Now Run your My Eclipse

5.Now Make a project Just Goto

File-------------->New------------->Java project

Name it Chat by viplove

6.Now These project can be seen in your screen just right to you

7.Right click on your project and goto

New--------------->Package----------------->

Now name the package

com.viplove

8.Now you can see these package in your project in src folder

9.Right click on the package and goto new

10.New------------>Class-------------->

And Build your classes as  shown below



CLIENT>JAVA

package com.viplove;


/*
	Author Viplove Gujrathi
	All the Right reserved by viplove gujrathi 
	Please contact me +91-880-288-7234
	or at viplove.gujrathi@gmail.com
*/
import java.io.*;
import java.net.*;

class Client extends Thread {
	
	private Socket socket;
	private String name;
	private BufferedReader input;
	private PrintStream output;
	private Server server;
	
	public Client(Socket socket, String name, Server server) 
	{
		this.socket = socket;
		this.name = name;
		this.server = server;
		try
		{
			input = new BufferedReader
			(new InputStreamReader(socket.getInputStream()));
			output = new PrintStream(socket.getOutputStream());
		}
		catch (Exception e) 
		{
			System.out.println(e);
		}
	}
	
	public void run() 
	{
		
		try
		{
			while (true)
			{
				String str = input.readLine();
			
				if (str == null)
					break;
				else
				{
					if (str.length() > 17 && str.substring(0, 18).equals("CLIENTNAMETOSERVER"))
					{
						setNavn(str.substring(19, str.length()));
						
						server.broadcast
						("" +
								"("
								+ Dato.getDato() 
								+ " " 
								+ Dato.getTid() 
								+ ")"
								+
								" <" 
								+ name
								+ "> connected to server");
						server.getLog().addToLog
						("" +
								"(" 
								+ socket.getInetAddress().getHostAddress()
								+ ")"
								+" <"
								+ name
								+ "> connected to server");
						sendUsers();
					}
					else if (str.length() > 11 && str.substring(0, 11).equals("¤#PRIVATE¤#"))
						privateMessage(str);
					else
					{
						server.broadcast
						("" +
								"("
								+ Dato.getDato() 
								+ " " 
								+ Dato.getTid()
								+ ")"
								+" <" 
								+ name
								+ "> " 
								+ str);
						server.getLog().addToLog
						("" +
								"(" 
								+ socket.getInetAddress().getHostAddress()
								+ ") <" 
								+ name +
								"> "
								+ str);
					}
				}
			}
			
			socket.close();
		}
		catch (Exception e)
		{
			server.getLog().addToLog
			("CLIENTFEJL " +
					"("
					+ socket.getInetAddress().getHostAddress()
					+ "): " 
					+ e);
		}
		finally
		{
			server.broadcast
			("" +
					"(" 
					+ Dato.getDato() +
					" " 
					+ Dato.getTid()
					
					+ ") <" 
					+ name
					+ "> disconnected from server");	
			server.getLog().addToLog
			("("
					+ socket.getInetAddress().getHostAddress()
					+ ") <" 
					+ name 
					+ "> disconnected from server");
			server.disconnectClient(this);
			sendUsers();
		}
	
	}
	
	public void transmit(String besked)
	{
		output.println(besked);
	}
	
	private void sendUsers()
	{
		server.broadcast("#GETUSERLIST# "
				+ server.getMonitor().getClientList()
				+ " " 
				+ server.getMonitor().getAntalClient());
	}
	
	private void privateMessage(String str)
	{
		String nameTemp = str.substring(11, str.length());
		String toName = "";
		String besked = "";
		Client toClient;
		boolean send = false;
		
		for (int i=0;i<nameTemp.length();i++)
		{
			if (nameTemp.charAt(i) == '¤' && nameTemp.charAt(i+1) == '#')
			{
				besked = nameTemp.substring(i+2, nameTemp.length());
				break;
			}
			else
				toName = toName + nameTemp.charAt(i);
		}
		
		System.out.println("Til " + toName);
		
		for (int j=0;j<server.getMonitor().getAntalClient();j++)
		{
			toClient = server.getMonitor().getClient(j);
			if (toClient.getNavn().equals(toName))
			{
				toClient.transmit("(" + Dato.getDato() + " " 
						+ Dato.getTid() + ") <privatebesked fra "
						+ getNavn() + "> " + besked);
				transmit
				("" +
						"(" + Dato.getDato() + " " + Dato.getTid()
						+ ") <privatebesked til " + toName + "> " 
						+ besked);
				server.getLog().addToLog
				("" +
						"("
						+ socket.getInetAddress().getHostAddress() 
						+ ") <" + name + "> sendte en privatebesked til"
						+" <" + toName + "> " 
						+ besked);
				System.out.println
				("Godt a client "
						+ toName + " / "
						+ toClient.getNavn()
						);
				send = true;
				break;
			}
		}
		
		if (!send)
		{
		System.out.println("Made by Viplove Gujrathi");	

	System.out.println(send);
			transmit("(" + Dato.getDato() 
					+ " " + Dato.getTid() 
					+ ") <" 
					+ toName 
					+ " er logget af så privatbeskeden er ikke sendt> ");
			server.getLog().addToLog
			("" +
					"("
					+ socket.getInetAddress().getHostAddress()
					+ ")"
					+" <" + name + "> sendte FORGÃVES en privatebesked til "
					+"<"+ toName + "> " + besked);
		}
	}
	System.out.println("Made by Viplove Gujrathi");
	public String getNavn()
	{
System.out.println("Made by Viplove Gujrathi");
		return this.name;
		}
	
		System.out.println("Made by Viplove Gujrathi");
	public void setNavn(String name)
	{ 
		this.name = name;
		}
}

ClientGUI.JAVA

package com.viplove;
/*
Nogenlunde log, skal finpudset !
*/

import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import javax.swing.border.*;
import java.net.*;
import java.io.*;
import java.applet.*;

public class ClientGUI
					extends JFrame 
						implements AdjustmentListener,
								   ActionListener,
								   WindowListener, 
								   KeyListener, 
								   Runnable
								   	{
										private Socket socket;
										private PrintStream output;
										private BufferedReader input;
										private JTextField textInput;
										private Thread internalThread;
										private JTextArea textareaChat;
										private JButton buttonSend;
										private JButton buttonConnect;
										private JLabel labStatus;
										private JLabel labHvem;
										private JPanel panUr;
										private JPanel panUser;
										private JPanel panChat;
										private JPanel panStatus;
										private TitledBorder titledBorder;
										private JScrollBar scrollBar;
										private JScrollPane scrollChat;
										private JScrollPane scrollClient;
										private JList listClient;
										private String host;
										private int port;
										private boolean running;
										private JMenuBar menu;
										private JMenu menuFunkt;
										private JMenu menuFiler;
										private JMenu menuHelp;
										private JMenuItem helpAbout;
										private JMenuItem funktStamdata;
										private JMenuItem filerGem;
										private JMenuItem filerQuit;
										private StamData stamData;
										private String filename;
										private FileManager fileManager;
										private AudioClip audioSound;
									    private URL urlDest;
										private String audioFile;
	
							public ClientGUI()
							{
								fileManager = new FileManager();
								filename = "data.dat";
								initialize();
								stamData = (StamData) fileManager.loadFile(filename);
								if (stamData == null)
									stamData = new StamData();
							}
	
							public void run()
							{
								while(running)
									{
									while (socket != null)
									{
										try 
										{
											String besked = input.readLine();
											if (besked != null)
											{
												if (besked.charAt(0) == '#' 
													&& besked.charAt(1) == 'G' 
														&& besked.charAt(2) == 'E'
															&& besked.charAt(3) == 'T')
																setClientList(besked, getAntal(besked));
										else
										{
											if(this.getFocusOwner() == null)
												if(stamData.getLyd())
													audioSound.play();
													scrollBar.setValue(scrollBar.getMaximum());	
													textareaChat.append(besked + "\n");
													scrollBar.setValue(scrollBar.getMaximum());
										}
									}
								else
									break;
										}
										catch(IOException e){}
										catch(Exception e)
										{
											System.out.println(e);
										}
									}
									repaint();
								}	
							}
	
							private void initialize()
							{
								getContentPane().setLayout(null);
								setTitle("Chat");
								setResizable(false);
								addWindowListener(this);
								setIconImage(Toolkit.getDefaultToolkit().getImage("icon.gif"));
								
								menu = new JMenuBar();
								menuFiler = new JMenu("Files");
								filerQuit = new JMenuItem("Exit");
								filerQuit.addActionListener(this);
								filerGem = new JMenuItem("Gem data");
								filerGem.addActionListener(this);
								menuFunkt = new JMenu("Functions");
								funktStamdata = new JMenuItem("Change data");
								funktStamdata.addActionListener(this);
								menuHelp = new JMenu("Help");
								helpAbout = new JMenuItem("About");
								helpAbout.addActionListener(this);
								menuHelp.add(helpAbout);
								menuFunkt.add(funktStamdata);
								menuFiler.add(filerGem);
								menuFiler.addSeparator();
								menuFiler.add(filerQuit);
								menu.add(menuFiler);
								menu.add(menuFunkt);
								menu.add(menuHelp);
								getContentPane().add(menu, BorderLayout.NORTH);
								
								setJMenuBar(menu);
		
								Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
								setSize(new Dimension(800, 520));
								setResizable(false);
								setLocation((screenSize.width-this.getWidth())/2,(screenSize.height-this.getHeight())/2);
								
								titledBorder = new TitledBorder("");
										
								panChat = new JPanel();
								panChat.setBounds(180, 20, 600, 400);
								panChat.setBorder(titledBorder);
								panChat.setLayout(null);
								getContentPane().add(panChat);
								
								panStatus = new JPanel();
								panStatus.setBounds(panChat.getX(), panChat.getHeight() + panChat.getY() + 10, panChat.getWidth(), 20);
								panStatus.setBorder(titledBorder);
								panStatus.setLayout(null);
								getContentPane().add(panStatus);
														
								panUser = new JPanel();
								panUser.setBounds(20, 20, 150, panChat.getHeight() + 10 + panStatus.getHeight());
								panUser.setBorder(titledBorder);
								panUser.setLayout(null);
								getContentPane().add(panUser);
										
								scrollChat = new JScrollPane();
								scrollChat.setBounds(10, 10, panChat.getWidth() - 20, (panChat.getHeight() - 50));
										
								textareaChat = new JTextArea();
								textareaChat.setBounds(5, 5, panChat.getWidth() - 30, (scrollChat.getHeight() - 10));
								textareaChat.setBorder(titledBorder);
								textareaChat.setLineWrap(true);
								textareaChat.setWrapStyleWord(true);		
								textareaChat.setEditable(false);
								scrollChat.add(textareaChat);
								
								scrollChat.setViewportView(textareaChat);        
						        panChat.add(scrollChat);
																
								scrollBar = scrollChat.getVerticalScrollBar();
								scrollBar.addAdjustmentListener(this);
										
								scrollClient = new JScrollPane();
								scrollClient.setViewportView(listClient);     
								scrollClient.setBounds(10, 30, 130, panChat.getHeight() + 10 + panStatus.getHeight() - 40);
								panUser.add(scrollClient);
								
								listClient = new JList();
								listClient.setBounds(10, 10, scrollClient.getWidth() - 20, scrollClient.getHeight() - 20);
								listClient.setEnabled(false);
								listClient.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
								scrollClient.add(listClient);
								
								labHvem = new JLabel();
								labHvem.setText("Send to:");
								labHvem.setBounds(10, 5, panUser.getWidth()-100, 20);
								panUser.add(labHvem);
										
								textInput = new JTextField();
								textInput.setBounds(10, (scrollChat.getHeight() + scrollChat.getY() + 10), (panChat.getWidth() - panChat.getY() - 220), 20);
								textInput.setText("Connect to server...");
								textInput.setEnabled(false);
								textInput.setRequestFocusEnabled(true);
								textInput.addKeyListener(this);
								panChat.add(textInput);
						
								buttonSend = new JButton();
								buttonSend.setText("Send");
								buttonSend.setBounds((textInput.getWidth() + textInput.getX() +10), (scrollChat.getHeight() + scrollChat.getY() + 10), 100, 20);
								buttonSend.addActionListener(this);
								buttonSend.setEnabled(false);
								panChat.add(buttonSend);
								
								buttonConnect = new JButton();
								buttonConnect.setText("Connect");
								buttonConnect.setBounds((textInput.getWidth() + textInput.getX() + buttonSend.getWidth() +20), (scrollChat.getHeight() + scrollChat.getY() + 10), 100, 20);
								buttonConnect.addActionListener(this);
								panChat.add(buttonConnect);
								
								labStatus = new JLabel();
								labStatus.setText("Status: Not Connected.. Press connect to connect to server");
								labStatus.setBounds(10, 0, panStatus.getWidth()-100, 20);
								panStatus.add(labStatus);
								
								loadSound();
							}
								
						public void loadSound() 
						{
							String audioFile = "notify.wav";
							try
							{
								urlDest = new URL("file:" + audioFile);
							}
							catch (MalformedURLException e){ System.err.println(e.getMessage()); }
		
							audioSound = Applet.newAudioClip(urlDest);
							}
	
						private  void connect()
						{
							if (stamData.getHost() != null 
									&& stamData.getHost().length() != 0 
										&& stamData.getPort().length() != 0)
								{
									labStatus.setText("Status: Connecting to server ...");
									try
									{
										if(stamData.getHost() != null)
											host = stamData.getHost();
										if(stamData.getPort() != null)	
											port = Integer.valueOf(stamData.getPort()).intValue();
										socket = new Socket(host, port);
										output = new PrintStream(socket.getOutputStream());
										input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
										
										internalThread = new Thread(this);
										internalThread.start();
										running = true;
										
										textInput.setEnabled(true);
										textInput.setText("");
										textInput.requestFocus();
										buttonSend.setEnabled(true);
										listClient.setEnabled(true);
										buttonConnect.setText("Disconnect");
										sendName();
										labStatus.setText("Status: Connected to " + host + " on port " + port);
									}
									catch(Exception e)
									{
										labStatus.setText("Status: Cannot connect to '" 
												+ host + " : " 
												+ port + "' try 'Change data'");		
									}
								}
								else
								{
									labStatus.setText("Status: Cannot connect to server try 'Change data'");		
								}
							}
	
							private void disconnect()
							{
								labStatus.setText("Status: Disconnecting to server ...");
								try
								{
									if (socket != null)
									{
										socket.close();
										socket = null;
									}
									running = false;
									textInput.setEnabled(false);
									textInput.setText("Connect to server...");
									buttonSend.setEnabled(false);
									listClient.setEnabled(false);
									setClientList("", 0);
									buttonConnect.setText("Connect");
									labStatus.setText("Status: Not Connected.. Press connect to connect to server");
									
								}
								catch(Exception e)
								{
									System.out.println("DISCONNECT: " + e);
								}
							}
									
									private void send()
									{
										if (textInput.getText().length() != 0)
										{
											String selectedName = (String) listClient.getSelectedValue();
											
											String besked = textInput.getText();
											
											if (selectedName != null && !selectedName.equals("Alle"))
											{
												besked = "¤#PRIVATE¤#" + selectedName + "¤#" + besked;
											}
											
											try
											{
												output.println(besked);
											}
											catch(Exception e)
											{
												System.out.println("SEND: " + e);
											}
											textInput.setText("");
											textInput.requestFocus();
										}
									}
									
									private void retStamdata()
									{
										StamDataGUI sg = new StamDataGUI(stamData);
										sg.show();
									}
									
									private void afslut()
									{
										running = false;
										disconnect();
										gemData(stamData);
										System.exit(0);
									}
									
									private void gemData(Object object)
									{
										try
										{
											fileManager.saveFile(object, filename);
										}
										catch(Exception e){}	
									}
									
									private void sendName()
									{
										output.println("CLIENTNAMETOSERVER " + stamData.getNavn());
									}
									
									public void actionPerformed(ActionEvent e)
									{
										if(e.getActionCommand()=="Connect")
											connect();
										else if(e.getActionCommand()=="Send")
											send();
										else if(e.getActionCommand()=="Disconnect")
											disconnect();
										else if(e.getActionCommand()=="Change data")			
											retStamdata();
										else if(e.getActionCommand()=="Gem data")
											gemData(stamData);			
										else if(e.getActionCommand()=="Exit")
											afslut();
									}
									
									private int getAntal(String besked)
									{
										String antal = "";
										String beskedTemp = besked;
										for (int i=beskedTemp.length()-1;i>0;i--)
										{
											if (beskedTemp.charAt(i) == 32)
												break;
											else
												antal = beskedTemp.charAt(i) + antal;
										}
										return convertAntal(antal);
									}
											
									private void setClientList(String besked, int antalInt)
									{
										String selectedName = (String) listClient.getSelectedValue();
										int selectedIndex = 0;
										
										String[] userList = new String[antalInt+1];
										userList[0] = "Alle";
										int nr = 1;
										String navn = "";
										String beskedTemp = besked;
										
										for (int i=beskedTemp.length()-1;i>0;i--)
										{
											if (beskedTemp.charAt(i) == 32)
											{
												besked = beskedTemp.substring(14,i);
												break;
											}
										}
										
										int antalTaeller = 1;
										
										for (int i=0;i<besked.length();i++)
										{
											if(besked.charAt(i) != '¤')
												navn = navn + besked.charAt(i);
											else
											{
												if (selectedName != null && navn.equals(selectedName))
													selectedIndex = antalTaeller;
													
												if (navn.equals(stamData.getNavn()))
												{
													nr++;
													navn = "";
												}
												else
												{
													userList[nr] = navn;
													nr++;
													navn = "";
												}
													
												antalTaeller++;
											}		
										}
										listClient.setListData(userList);
										listClient.setSelectedIndex(selectedIndex);
									}
									
									private int convertAntal(String antal)
									{
										return Integer.valueOf(antal).intValue();
									}
										
									public void adjustmentValueChanged(AdjustmentEvent e){}
									
									public void windowClosed(WindowEvent e){}
								    public void windowIconified(WindowEvent e){}
								    public void windowDeiconified(WindowEvent e){}
								    public void windowActivated(WindowEvent e){}
								    public void windowDeactivated(WindowEvent e){}
								    public void windowOpened(WindowEvent e){}
								    public void windowClosing(WindowEvent e)
									{
										try{
											socket.close();
											socket = null;
										}
										catch(IOException ex){
											System.out.println(ex);
										}
										finally
										{
											afslut();
								    	}
								    }
									
									public void keyReleased(KeyEvent e){}
								    public void keyTyped(KeyEvent e){}
								    public void keyPressed(KeyEvent e)
									{
										if(e.getKeyCode()==10)
											send();	
								    }
								
								}

ClientMOnitor.JAVA

package com.viplove;
import java.util.Vector;

public class ClientMonitor
{
	private Vector clientVector;
	
			public ClientMonitor()
			{
				clientVector = new Vector();
			}
			
			public synchronized void addClient(Client client)
			{
				clientVector.add(client);
			}
			
			public synchronized void delClient(Client client)
			{
				clientVector.removeElement(client);
			}
			
			public synchronized Client getClient(int index)
			{
				return (Client) clientVector.elementAt(index);
			}
			
			public int getAntalClient()
			{
				return clientVector.size();
				}
		
		public String getClientList()
		{
			String clients = "";
			for (int i=0;i<getAntalClient();i++)
				clients = clients + getClient(i).getNavn() + "¤";
			return clients;
		}
	}

DATO.JAVA

package com.viplove;
import java.util.*;

class Dato
{
	private Dato()
			{
		System.out.println("Author Viplove");
			}
				
						public static String getDato()
				{
					Calendar dato = Calendar.getInstance();
					
					String dag = String.valueOf(dato.get(Calendar.DATE));
					if (dag.length() <= 1)
						dag = "0" + dag;
					
					String maaned = String.valueOf((dato.get(Calendar.MONTH)+1));
					if (maaned.length() <= 1)
						maaned = "0" + maaned;
					
					String aar = String.valueOf(dato.get(Calendar.YEAR)).substring(2,4);
					
					return (dag + "-" + maaned + "-" + aar);
				}
							
				public static String getTid()
				{
					Calendar dato = Calendar.getInstance();
					
					String tid = "";
					if(dato.get(Calendar.AM)>0)
						tid = "" + (dato.get(Calendar.HOUR)+12);
					else
						tid = "" + dato.get(Calendar.HOUR);
			
					if(dato.get(Calendar.MINUTE)>9)	
						tid = tid + ":" + dato.get(Calendar.MINUTE);
					else
						tid = tid + ":0" + dato.get(Calendar.MINUTE);
					
					if(dato.get(Calendar.SECOND)>9)
						tid = tid + ":" + dato.get(Calendar.SECOND);
					else
						tid = tid + ":0" + dato.get(Calendar.SECOND);
					return tid;
				}
			}

FILEMANAGER.JAVA

package com.viplove;

import java.io.*;

class FileManager
{
	public FileManager()
	{
	}constructor
	
			public void saveFile(Object object, String filename)
			{
				try 
				{
						File outFile = new File(filename);
						FileOutputStream outFileStream = new FileOutputStream(outFile);
						ObjectOutputStream outObjectStream = new ObjectOutputStream(outFileStream);
						outObjectStream.writeObject(object);
						outObjectStream.close();
									}try			
							catch(IOException e)
							{
								System.out.println(e);
								object = null;
							}catch		
						}saveFile
							
						public Object loadFile(String filename)
						{
							Object object;
							
							try
							{
									File inFile = new File(filename);
									FileInputStream inFileStream = new FileInputStream(inFile);
									ObjectInputStream objectInputStream = new ObjectInputStream(inFileStream);
									object = objectInputStream.readObject();
									objectInputStream.close();
							}try
							catch(Exception e)
							{
								System.out.println(e);
								object = null;
							}catch
							
							return object;
						}loadFile
					}class

LOG.JAVA

package com.viplove;

import java.io.*;
import java.util.Vector;

public class Log implements Serializable
{
	private File file;
	private FileOutputStream outFile;
	private PrintWriter outStream;
	private Vector vectorBesked;

	public Log()
	{
		file = new File("log/" + "log(" + Dato.getDato() + ").txt");
		vectorBesked = new Vector();
	}
	
	public void addToLog(String besked)
	{
		besked = "(" + Dato.getDato() + " " + Dato.getTid() + ") " + besked;
		
		vectorBesked.add(besked);
		
		try
		{
			outFile = new FileOutputStream(file);
			outStream = new PrintWriter(outFile);
		}
		catch(Exception e){ System.out.println(e); }
		
		for (int i=vectorBesked.size();i>0;i--)
			outStream.println(vectorBesked.elementAt(i-1));
			
		closeLog();
	}
	
	public void closeLog()
	{
		outStream.close();	
	}
}

MAIN.JAVA

package com.viplove;

public class Main
{
	public static void main(String args[])
	{
		ClientMonitor monitor = new ClientMonitor();
		Server server = new Server(monitor, 3000);
		server.start();
		ClientGUI sc = new ClientGUI();
		sc.show();
	}

}

SERVER.JAVA

package com.viplove;

import java.io.*;
import java.net.*;

public class Server extends Thread
{
	private ClientMonitor monitor;
	private Client client;
	private int port;
	private Log log;
	
	public Server(ClientMonitor monitor, int port)
	{
		this.monitor = monitor;
		this.port = port;
		log = new Log();
	}
	
	public void run()
	{
		try 
		{
			ServerSocket s = new ServerSocket(port);
			log.addToLog
			("Server run on "
					+ s.getInetAddress().getHostAddress()
					+ ":" + port);
			while (true) 
			{
				Socket incoming = s.accept();
				client = new Client(incoming, incoming.getInetAddress().getHostAddress(), this);
				client.start();
				client.transmit
				("" +
						"("
						+ Dato.getDato() 
						+ " " 
						+ Dato.getTid()
						+ ") Velkommen til serveren der er iøjeblikket " 
						+ getMonitor().getAntalClient() 
						+ " brugere på serveren.");
				monitor.addClient(client);
			}
		}
		catch (Exception e)
		{
			log.addToLog("SERVERFEJL: " + e);
		}
	}
	
	public void broadcast(String besked)
	{
		for(int i=0;i<monitor.getAntalClient();i++)
		{
			monitor.getClient(i).transmit(besked);
		}
	}
	
	public void disconnectClient(Client client)
	{
		monitor.delClient(client);
	}
	
	public ClientMonitor getMonitor()
	{
		return monitor; 
	}
	public void setMonitor(ClientMonitor monitor)
	{
		this.monitor = monitor; 
	}
	public Log getLog()
	{ 
		return log;
	}
	public void setLog
	(Log log)
	{
		this.log = log;
		}
}

StamData.JAVA

package com.viplove;

import java.io.*;

public class StamData implements Serializable
{
	private String name;
	private String host;
	private String port;
	private boolean lyd;
	
	public StamData()
	{
		this.name = null;
		this.host = null;
		this.port = null;
		this.lyd = true;		
	}
	
	public StamData(String navn, String host, String port, boolean lyd)
	{
		this.name = name;
		this.host = host;
		this.port = port;
		this.lyd = lyd;
	}
	
	public String getNavn(){ return name; }
	public String getHost(){ return host; }
	public String getPort(){ return port; }
	public boolean getLyd(){ return lyd; }
	
	public void setName(String navn){ this.name = name; }
	public void setHost(String host){ this.host = host; }
	public void setPort(String port){ this.port = port; }
	public void setLyd(boolean lyd){ this.lyd = lyd; }
}

StamDataGui.JAva

package com.viplove;

import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import javax.swing.border.*;
import java.net.*;
import java.io.*;

public class StamDataGUI extends JDialog implements ActionListener, WindowListener, KeyListener
{
	private JLabel labName;
	private JLabel labHost;
	private JLabel labPort;
	private JLabel labLyd;
	private JTextField textNavn;
	private JTextField textHost;
	private JTextField textPort;
	private TitledBorder titledBorder;
	private JPanel panStamData;
	private JPanel panKnap;
	private JButton buttonOk;
	private JButton buttonFortryd;
	private JRadioButton radioJa;
	private JRadioButton radioNej;
	private ButtonGroup groupSound;
	private StamData stamData;
	
	public StamDataGUI(StamData stamData)
	{
		this.stamData = stamData;
		initialize();
	}
	
	private void initialize()
	{
		getContentPane().setLayout(null);
		setTitle("Change data");
		addWindowListener(this);
		addKeyListener(this);
		setModal(true);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setSize(new Dimension(300, 255));
		setResizable(false);
		setLocation((screenSize.width-this.getWidth())/2,(screenSize.height-this.getHeight())/2);
		
		titledBorder = new TitledBorder("");
		
		panStamData = new JPanel();
		panStamData.setBounds(20, 20, this.getWidth() - 40, this.getHeight() - 115);
		panStamData.setBorder(titledBorder);
		panStamData.setLayout(null);
		getContentPane().add(panStamData);
		
		panKnap = new JPanel();
		panKnap.setBounds(20, panStamData.getHeight() + panStamData.getY() + 10, panStamData.getWidth() , 40);
		panKnap.setBorder(titledBorder);
		panKnap.setLayout(null);
		getContentPane().add(panKnap);
		
		labName = new JLabel();
		labName.setBounds(10, 10, 80, 20);
		labName.setText("Navn: ");
		panStamData.add(labName);
		
		labHost = new JLabel();
		labHost.setBounds(10, 40, 80, 20);
		labHost.setText("Host: ");
		panStamData.add(labHost);
		
		labPort = new JLabel();
		labPort.setBounds(10, 70, 80, 20);
		labPort.setText("Port: ");
		panStamData.add(labPort);
		
		labLyd = new JLabel();
		labLyd.setBounds(10, 100, 80, 20);
		labLyd.setText("Use sounds? ");
		panStamData.add(labLyd);
		
		radioJa = new JRadioButton();
		radioJa.setBounds(120, 100, 50, 20);
		radioJa.setText("Yes");
		radioJa.setFocusPainted(false);
		panStamData.add(radioJa);
				
		radioNej = new JRadioButton();
		radioNej.setBounds(180, 100, 50, 20);
		radioNej.setText("No");
		radioNej.setFocusPainted(false);
		panStamData.add(radioNej);
		
		if(stamData.getLyd())
			radioJa.setSelected(true);
		else
			radioNej.setSelected(true);
		
		groupSound = new ButtonGroup();
		groupSound.add(radioJa);
		groupSound.add(radioNej);
					
		textNavn = new JTextField();
		textNavn.setBounds(100, 10, 150, 20);
		textNavn.setText(stamData.getNavn());
		textNavn.addKeyListener(this);
		panStamData.add(textNavn);
		
		textHost = new JTextField();
		textHost.setBounds(100, 40, 150, 20);
		textHost.setText(stamData.getHost());
		textHost.addKeyListener(this);
		panStamData.add(textHost);
		
		textPort = new JTextField();
		textPort.setBounds(100, 70, 150, 20);
		textPort.setText(stamData.getPort());
		textPort.addKeyListener(this);
		panStamData.add(textPort);
		
		buttonOk = new JButton();
		buttonOk.setText("Ok");
		buttonOk.setBounds(25, 10, 100, 20);
		buttonOk.addActionListener(this);
		panKnap.add(buttonOk);		
		
		buttonFortryd = new JButton();
		buttonFortryd.setText("Fortryd");
		buttonFortryd.setBounds(buttonOk.getWidth() + buttonOk.getX() + 10 , 10, 100, 20);
		buttonFortryd.addActionListener(this);
		panKnap.add(buttonFortryd);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand()=="Ok")
			ok();
		else
			fortryd();	
	}
	
	private void ok()
	{
		stamData.setName(textNavn.getText());	
		stamData.setHost(textHost.getText());
		stamData.setPort(textPort.getText());
		
		if(radioJa.isSelected())
			stamData.setLyd(true);
		else
			stamData.setLyd(false);	
		
		dispose();
	}
	
	private void fortryd()
	{
		dispose();
	}
	
	public void windowClosed(WindowEvent e){}
    public void windowIconified(WindowEvent e){}
    public void windowDeiconified(WindowEvent e){}
    public void windowActivated(WindowEvent e){}
    public void windowDeactivated(WindowEvent e){}
    public void windowOpened(WindowEvent e){}
    public void windowClosing(WindowEvent e){ fortryd(); }
	
	public void keyReleased(KeyEvent e){}
    public void keyTyped(KeyEvent e){}
    public void keyPressed(KeyEvent e)
	{
		if(e.getKeyCode()==10)
			ok();
		else if (e.getKeyCode() == 27)
			fortryd();
    }
}

NOTE

To run These project you would need to include log folder into src which consist of txt files

to get that txt files you can contact me at

viplove.gujrathi@gmail.com

viplove09may@gmail.com

viplovegujrathi@rediffmail.com

viplovegujrathi@hotmail.com

viplovegujrathi@indiatimes.com

love.viplove@yahoo.com

Or else call me on +91-9702-145-614

Comments 4 comments

Sue Adams profile image

Sue Adams 5 years ago from Andalusia

Too technical for me


Ashish jain 5 years ago

abey ye kahan se tipa...

ramayan ki coding ki hai kya??? :P


Anonmnous 5 years ago

Really


Giodwin 5 years ago

Nice.....appreciate it

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working