I am trying to create chating system in java using nio channels.I created simple gui for client login and registration .i also used databse for storing client detailes.but whn i click on the login button ,the chating frame does not appear.
here is my client-login code
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import DataBase.DBConnection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
public class Client_login extends JFrame
{
JButton login,Register;
JLabel label1,label2,label3;
JTextField text1,text2,text3;
DBConnection dbc=new DBConnection();
ResultSet rs=null;
int flag=0;
ImageIcon image = new ImageIcon( "Desert.jpg" );
JPanel jPanel1=new JPanel();
public Client_login()
{
setTitle("Login Form");
setLayout(null);
label1=new JLabel();
label1.setText("Username");
text1=new JTextField(15);
label2=new JLabel();
label2.setText("Password");
text2=new JPasswordField(15);
label3=new JLabel();
text3=new JTextField(15);
login=new JButton("Login");
Register=new JButton("Register");
label3.setText("ServerIP");
label3.setBounds(100,70,100,20);
text3.setBounds(200,70,200,20);
label1.setBounds(100,100,100,20);
text1.setBounds(200,100,200,20);
label2.setBounds(100,130,100,20);
text2.setBounds(200,130,200,20);
login.setBounds(130,169,100,20);
Register.setBounds(240, 169, 100,20);
add(label1);
add(text1);
add(label2);
add(text2);
add(login);
add(Register);
add(label3);add(text3);
setVisible(true);
setSize(500,400);
login.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
String value1=text1.getText();
String value2=text2.getText();
String uname=null;String pass=null;
System.out.println(value1+""+value2);
if(value1.equals("")|value2.equals(""))
JOptionPane.showMessageDialog(rootPane, "Enter username and password");
else{
try
{
String sql="select * from Client_registration where usrname='"+value1+"' and password='"+value2+"'";
rs=dbc.select(sql);
if(rs.next())
{
uname=rs.getString("usrname");
pass=rs.getString("password");
System.out.println("from database"+uname);
try
{
setVisible(false);
ChatClient cc=new ChatClient(uname,"localhost");
System.out.println("from database "+uname);
//OnLineUsers ous=new OnLineUsers(uname,"localhost");
//Client_form cf=new Client_form(uname,"localhost");
try {
BufferedImage image = null;
//frame.setIconImage(Toolkit.getDefaultToolkit().get Image("chat.gif"));
// image = ImageIO.read(cf.getClass().getResource("chat.gif") );
//cf.setIconImage(image);
}catch(Exception e)
{
}
} catch (Exception ex) {
Logger.getLogger(Client_login.class.getName()).log (Level.SEVERE, null, ex);
}
//JOptionPane.showMessageDialog(rootPane, "Login successfully");
}
else{
JOptionPane.showMessageDialog(rootPane, "Invalid login");
text1.setText("");
text2.setText("");
}
}
catch(SQLException e){
}}
}
});
Register.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
Client_register cr=new Client_register();
}
});
}
//ImageIcon image = new ImageIcon( "C:/rose.jpg" );
public static void main(String[] args) {
//new Client_login();
Client_login frame=new Client_login();
try {
BufferedImage image = null;
//frame.setIconImage(Toolkit.getDefaultToolkit().get Image("chat.gif"));
image = ImageIO.read(frame.getClass().getResource("chat.gi f"));
frame.setIconImage(image);
}
catch(Exception e){
}
try{
BufferedImage image = ImageIO.read(frame.getClass().getResource("Desert. gif"));
JLabel myLabel = new JLabel();
myLabel = new JLabel(new ImageIcon(image));
frame.getContentPane().add(myLabel);
}
catch(Exception e){
}
}
}
and my chating_frame code is
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import DataBase.DBConnection;
import java.sql.*;
/**
*
* @author appuraj.kr
*/
public class ChatClient extends JFrame implements ActionListener
{
public String uname;
public JTextArea taMessages;
public JTextField tfInput;
public JButton btnSend,btnExit;
public BufferedReader br;
int k=0;
int f=0;
int flag=0;
ResultSet rs=null;
ResultSet rs1=null;
String sql=null;
String user_name=null;
// DBConnection dbc=new DBConnection();
//Client_start cst=new Client_start();
String sname=null;
public ChatClient(String uname,String servname) throws IOException
{
super(uname); // set title for frame
this.uname = uname;
sname=servname;
//System.out.println(this.uname);
// br=new BufferedReader(new InputStreamReader(System.in));
/*if(f==0){
try{
sql="insert into Client_registration(usrname) values('"+uname+"')";
flag=dbc.insert(sql);
if(flag>0)
System.out.println("inserted");}
catch(SQLException e){
e.printStackTrace();
}*/
buildInterface();
//cst.connect(user_name, sname);
//System.out.println(this.uname);
// new MessagesThread().start();
}
public void buildInterface()
{
btnSend = new JButton("Send");
btnExit = new JButton("Exit");
taMessages = new JTextArea();
taMessages.setRows(10);
taMessages.setColumns(50);
taMessages.setEditable(false);
tfInput = new JTextField(50);
JScrollPane sp = new JScrollPane(taMessages, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPa ne.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(sp,"Center");
JPanel bp = new JPanel( new FlowLayout());
bp. add(tfInput);
bp.add(btnSend);
bp.add(btnExit);
add(bp,"South");
btnSend.addActionListener(this);
btnExit.addActionListener(this);
setSize(500,300);
setVisible(true);
pack();
//Client_start cst=new Client_start();
//cst.connect(user_name, sname);
}
public void actionPerformed(ActionEvent evt)
{
String line=null;
String data=null;
if ( evt.getSource() == btnExit )
{
String msg1="end";
//buffer1=ByteBuffer.wrap(new String(msg1).getBytes());
//pw.println("end"); // send end to server so that server know about the termination
System.exit(0);
} else
{
// send message to server
// pw.println(tfInput.getText());
// System.out.println("code here");
//System.out.println(tfInput.getText());
line=tfInput.getText();
String incoming=null;
try
{
// cst.send_mesg(line);
// incoming=cst.read_mesg();
//
taMessages.append("server:"+data+ "\n");
}
catch (Exception ex)
{
Logger.getLogger(ChatClient.class.getName()).log(L evel.SEVERE, null, ex);
}
tfInput.setText("");
}
}
public static void main(String[] args) {
{
String name=null;
// take username from user
do{
name = JOptionPane.showInputDialog(null,"Enter your name :", "Username",JOptionPane.PLAIN_MESSAGE);
if(name.equals(""))
JOptionPane.showMessageDialog(null, "username already exists");
} while(name.equals(""));
String servername = "localhost";
try
{
new ChatClient( name ,servername);
} catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}