Im creating a Lan messenger in java but seem to have some problems;
first the elements of the GUI do not load when the window pops up (i have to strech the window for them to appear(frame size is not the problem)).

moreover the program just hangs wen i try to use (GUI freezes).
i will be including database connectivity to create users list with passwords later;this shall be done later once i get the messenger to actually work.


quick help would be appreciated because i have a dead line to meet.
thanks


:confused:

Post the code here.

shot in the dark but you mention the GUI freezes... The GUI will freeze if its not correctly called at runtime from the main method using a different thread. Without seeing code I can't comment further.

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


class messenger extends WindowAdapter 
{
JFrame frm;
JPanel p;
JTextField txtb;

JLabel lbl;
JButton b;
JTextArea txta;

JLabel lbl2;
JLabel lbl3;
JLabel lbl4;
JScrollBar scrbar;

public messenger()
{


System.out.println("hello");
frm=new JFrame("Instant Messenger");

p=new JPanel();

txtb=new JTextField(50);

lbl=new JLabel("Message:");
scrbar=new JScrollBar();

lbl2=new JLabel("Chat Box:");

b=new JButton("Send");

txta=new JTextArea(5,5);



p.setLayout(null);

lbl2.setLocation(95,42);
lbl2.setSize(100,100);
p.add(lbl2);

txta.setLocation(95,100);
txta.setSize(500,250);
p.add(txta);

lbl.setLocation(95,400);
lbl.setSize(100,100);
p.add(lbl);

txtb.setLocation(95,475);
txtb.setSize(500,80);
p.add(txtb);



b.setLocation(600,520);
b.setSize(75,25);
b.setBackground(SystemColor.controlDkShadow);
p.add(b);

frm.show();
frm.setSize(720,600);
frm.setLocation(200,0);
p.setBackground(Color.yellow);
p.repaint();


frm.getContentPane().add(p);
frm.addWindowListener(this);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}

}


public class start extends WindowAdapter implements ActionListener
{
String pass_word="";
String usr_name="";

int countError=0;
JFrame frame1;
JPanel pan1;

JLabel lbl1;
JTextField usr,ip_Var;
JLabel lbl2,lbl3;
JPasswordField pwd;
JButton b1;

public start()
{
frame1=new JFrame("Login");
pan1=new JPanel();

usr=new JTextField(10);
pwd=new JPasswordField(6);
ip_Var=new JTextField(10);

lbl1=new JLabel("Username:");
lbl2=new JLabel("Password:");
lbl3=new JLabel("ServerIP:");
b1=new JButton("Sumbit");

pan1.setLayout(null);
                                                          
lbl1.setLocation(50,50);
lbl1.setSize(100,100);
pan1.add(lbl1);


usr.setLocation(125,90);
usr.setSize(75,25);
pan1.add(usr);


lbl2.setLocation(50,100);
lbl2.setSize(100,100);
pan1.add(lbl2);


pwd.setLocation(125,140);
pwd.setSize(75,25);
pan1.add(pwd);

lbl3.setLocation(50,155);
lbl3.setSize(100,100);
pan1.add(lbl3);

ip_Var.setLocation(125,200);
ip_Var.setSize(75,25);
pan1.add(ip_Var);


b1.setLocation(120,300);
b1.setSize(75,25);
pan1.add(b1);
b1.addActionListener(this);


frame1.show();
frame1.setSize(200,500);
frame1.setLocation(500,100);



frame1.getContentPane().add(pan1);
frame1.addWindowListener(this);
usr_name=usr.getText();
pass_word=String.valueOf(pwd.getPassword());
System.out.println(pass_word);
}

public void actionPerformed(ActionEvent et)
{


String p_Var;
int port_Var;

//if(pass_word.compareTo("pass")==0) 
//{

try
{

Socket toServer;
toServer=new Socket(ip_Var.getText(),80);
ObjectOutputStream streamToServer=new ObjectOutputStream(toServer.getOutputStream());
//Details data=new Details();
streamToServer.writeObject(usr_name);
BufferedReader fromServer=new BufferedReader(new InputStreamReader(toServer.getInputStream()));

String status=fromServer.readLine();
JOptionPane.showMessageDialog(null,"You have been authenticated by the server as:"+status);
System.out.println("Login successful!");
messenger begin=new messenger();
frame1.hide();
}




catch(InvalidClassException e)
{


System.out.println("The Details class is invalid"+e);


}
catch(NotSerializableException e)
{
System.out.println("The object is not serializable"+e);
}


catch (IOException e)
{

System.out.println("Cannot write to the server"+e);

}


//}


//else
//{
//countError=countError+1;
//System.out.println(pass_word);
//JOptionPane.showMessageDialog(null,"Password Incorrect!!");

//if(countError>3)
//{

//JOptionPane.showMessageDialog(null,"Password has been input incorrectly more than 3 times;Do not abuse my software!! ");

//System.exit(0);

//}

//}





}


public void windowClosing(WindowEvent e)
{
System.exit(0);
}

public static void main(String ar[])
{
start obj=new start();
}




}

heres the code
> :(

yup. Doing everything in one thread, and that being the Swing Event Dispatcher thread.
Guaranteed to cause application locks while waiting for things to happen on the network.

yup. Doing everything in one thread, and that being the Swing Event Dispatcher thread.
Guaranteed to cause application locks while waiting for things to happen on the network.

++
Exactly. If you don't want longer-running tasks tying up your GUI, you'll have to execute those in a separate thread.

Keep in mind also, don't try updating your GUI from that separate thread either or you'll see inconsistent behavior of your GUI components. Any code running in a separate thread that needs to update GUI components should be pushed on to the event dispatch thread as a new Runnable() via EventQueue.invokeLater().

ok i updated the code a bit ,but still have some issues with it running..it dosent seem to connect with the server..plz help.

Heres the new client code

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


class messenger extends WindowAdapter implements ActionListener
{
JFrame frm;
JPanel p;
JTextField txtb,txtip;

JLabel lbl,lbl1;
JButton b,bcon;
JTextArea txta;

JLabel lbl2;
JLabel lbl3;
JLabel lbl4;
JScrollBar scrbar;
PrintStream ps;
BufferedReader br1;
	public messenger()
	{


	System.out.println("hello");
	frm=new JFrame("Instant Messenger");
	lbl1=new JLabel("Ip Address");

	p=new JPanel();

	txtb=new JTextField(50);
	txtip=new JTextField(50);
	lbl=new JLabel("Message:");
	scrbar=new JScrollBar();

	lbl2=new JLabel("Chat Box:");

	b=new JButton("Send");
	bcon=new JButton("Connect");
	txta=new JTextArea(5,5);
	txta.setEnabled(false);


	p.setLayout(null);

	lbl1.setLocation(95,7);
	lbl1.setSize(100,100);
	p.add(lbl1);
	
	txtip.setLocation(195,42);
	txtip.setSize(175,25);
	p.add(txtip);

	bcon.setLocation(400,42);
	bcon.setSize(100,25);
	bcon.addActionListener(this);
	p.add(bcon);

	lbl2.setLocation(95,136);
	lbl2.setSize(100,100);
	p.add(lbl2);
	
	txta.setLocation(100,200);
	txta.setSize(400,200);
	p.add(txta);

	lbl.setLocation(95,400);
	lbl.setSize(100,100);
	p.add(lbl);

	txtb.setLocation(95,475);
	txtb.setSize(500,80);
	p.add(txtb);



	b.setLocation(600,520);
	b.setSize(75,25);
	b.setBackground(SystemColor.controlDkShadow);
	p.add(b);


	

	frm.show();
	frm.setSize(720,600);
	frm.setLocation(200,0);
	p.setBackground(Color.yellow);
	p.repaint();


	frm.getContentPane().add(p);
	frm.addWindowListener(this);




	try
			{
			
	
			Socket sc=new Socket("10.10.5.5",100);
		
		InputStreamReader In=new InputStreamReader(sc.getInputStream());
		br1=new BufferedReader(In);
		
		ps=new PrintStream(sc.getOutputStream()); 
		ps.println("Trying to connect");
			for(;;)
			{
				txta.setText(txta.getText()+"\n"+br1.readLine());
			}
		}
		catch(Exception e){System.out.println(e);}
	}

	
	public void actionPerformed(ActionEvent et)
	{

		if(et.getSource()==bcon)
		{
		String p_Var=txtip.getText();
		

	
			
		}

		if(et.getSource()==b)
		{
		String s1=txtb.getText();
		ps.println(s1);
		txtb.setText(" ");
		}

	}
		public void windowClosing(WindowEvent e)
		{
		System.exit(0);
		}

}


public class start extends WindowAdapter implements ActionListener
{
String pass_word="";
String usr_name="";

int countError=0;
JFrame frame1;
JPanel pan1;

JLabel lbl1;
JTextField usr,ip_Var;
JLabel lbl2,lbl3;
JPasswordField pwd;
JButton b1;

	public start()
	{
	frame1=new JFrame("Login");
	pan1=new JPanel();

	usr=new JTextField(10);
	pwd=new JPasswordField(6);
	ip_Var=new JTextField(10);

	lbl1=new JLabel("Username:");
	lbl2=new JLabel("Password:");
	lbl3=new JLabel("ServerIP:");
	b1=new JButton("Sumbit");

	pan1.setLayout(null);
                                                          
	lbl1.setLocation(50,50);
	lbl1.setSize(100,100);
	pan1.add(lbl1);


	usr.setLocation(125,90);
	usr.setSize(75,25);
	pan1.add(usr);


	lbl2.setLocation(50,100);
	lbl2.setSize(100,100);
	pan1.add(lbl2);


	pwd.setLocation(125,140);
	pwd.setSize(75,25);
	pan1.add(pwd);

	lbl3.setLocation(50,155);
	lbl3.setSize(100,100);
	pan1.add(lbl3);

	ip_Var.setLocation(125,200);
	ip_Var.setSize(75,25);
	pan1.add(ip_Var);


	b1.setLocation(120,300);
	b1.setSize(75,25);
	pan1.add(b1);
	b1.addActionListener(this);


	frame1.show();
	frame1.setSize(200,500);
	frame1.setLocation(500,100);



	frame1.getContentPane().add(pan1);
	frame1.addWindowListener(this);
	usr_name=usr.getText();
	pass_word=String.valueOf(pwd.getPassword());
	System.out.println(pass_word);
	}

	public void actionPerformed(ActionEvent et)
	{
		messenger msg=new messenger();
		frame1.hide();
	}

	

//}


//else
//{
//countError=countError+1;
//System.out.println(pass_word);
//JOptionPane.showMessageDialog(null,"Password Incorrect!!");

//if(countError>3)
//{

//JOptionPane.showMessageDialog(null,"Password has been input incorrectly more than 3 times;Do not abuse my software!! ");

//System.exit(0);

//}

//}


	public void windowClosing(WindowEvent e)
	{
	System.exit(0);
	}

	public static void main(String ar[])
	{	
	start obj=new start();
	}




}

The server code is :

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
class server extends Frame implements ActionListener
{
TextField tf;
PrintStream ps;
BufferedReader br1;
server()
{
Button b1;
TextArea ta=new TextArea(10,30);
setLayout(new FlowLayout());
b1=new Button("Submit");
tf=new TextField(20);
add(ta);
add(tf); 
add(b1);
setTitle("Server");
ta.setEnabled(false);
setSize(300,300);
b1.addActionListener(this);

show();

try
{
ServerSocket so=new ServerSocket(100);
ta.setText("Server ready");
//when a server secures a connection from the client, the accept() method of ServerSocket class accepts the connection
Socket sc=so.accept();
//getInputStream()-reading from the socket
InputStreamReader In=new InputStreamReader(sc.getInputStream());
br1=new BufferedReader(In);//BufferedReader()-to read from the socket
//getOutputStream()-writing to a socket
ps=new PrintStream(sc.getOutputStream()); //PrintStream() write into a socket
ps.println("Connection ok");
for(;;)
{
ta.setText(ta.getText()+"\n"+br1.readLine());
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public void actionPerformed(ActionEvent e)
{
String s1=tf.getText();
ps.println(s1);
tf.setText(" ");
}
public static void main(String s[])
{
new server();
}
}

-thanks

Perhaps you should use 'non blocking socket ' whatever that means .. so its .nio* or something like that ... succes!

huh i didnt understand ,please elaborate....

import java.applet.Applet;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.*;
import java.awt.*;

public class nio2 extends Applet implements Runnable{
	static final long serialVersionUID = -116069779446114664L;
	
	Thread st;
	SocketChannel sc;
	ByteBuffer bb;
	CharBuffer cb,cb2;
	Charset cset;
	CharsetDecoder dec;
	CharsetEncoder enc;
	String readsocket;
	int ls;
	
	public void init()
	{	
		
try{
			
			
			sc=SocketChannel.open();
			sc.configureBlocking(false);
			sc.connect(new InetSocketAddress("84.244.8.225",80));
			while(!sc.finishConnect()){
				
			}
			
			
			
			}catch(Exception e){}

		
		st=new draad();
		st.start();
	}
	
	class draad extends Thread{
		public void run(){
			try{
		
		bb=ByteBuffer.allocateDirect(60000);
		bb.clear();
		ls=sc.read(bb);
		bb.flip();
			java.nio.charset.Charset tcs=java.nio.charset.Charset.forName("us-ascii");
			java.nio.charset.CharsetDecoder tcsd=tcs.newDecoder();
			cb2=tcsd.decode(bb);
			
			
		 readsocket=cb2.toString();
		
		}catch(Exception e){}
	}
	}
	public void run(){
		
		
	}
	public boolean keyUp(Event ek,int k){
		if(k==Event.ENTER){
			try{
				
				bb=ByteBuffer.allocate(1024);
				cb=bb.asCharBuffer();
				cb.put("Hello world");
				cb.flip();
				java.nio.charset.Charset tcs2=java.nio.charset.Charset.forName("us-ascii");
				java.nio.charset.CharsetEncoder tcsd2=tcs2.newEncoder();
				bb=tcsd2.encode(cb);
				sc.write(bb);
				
			}catch(Exception ex5){}
		}

		return true;

		}
}

wot does this actually do and where in my program can i implement it?

Read through the code and see what it's doing. You're going to have to write some code yourself, there's not really going to be a "cut n' paste" solution on this forum. I would suggest you start with some easier basic tutorials on Java networking.

http://java.sun.com/docs/books/tutorial/networking/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.