It is Login Dialog
//LoginDialog Class
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class LoginDialog extends JDialog implements ActionListener,Constants{
String Username=null,Password=null,Server=null;
JLabel label1,label2,label3,label4;
JTextField user,server,port;
JPasswordField password;
JButton ok,cancel;
Socket client = null;
ServerSocket ss=null;
PrintWriter out = null;
BufferedReader in = null;
String line;
//Constructor
public LoginDialog(){
Container container = getContentPane();
container.setLayout(new FlowLayout(FlowLayout.LEFT));
label1= new JLabel("Login name :");
label1.setBounds(10,10,80,20);
label2= new JLabel(" Password :");
label2.setBounds(10,40,80,20);
user= new JTextField(20);
user.setBounds(100,10,100,20);
password=new JPasswordField(20);
password.setBounds(100,40,100,20);
label3= new JLabel(" Server :");
label3.setBounds(10,70,80,20);
label4= new JLabel(" Port :");
label4.setBounds(10,100,80,20);
server= new JTextField(SERVER_HOST);
server.setBounds(100,70,100,20);
port=new JTextField(SERVER_PORT+"");
port.setBounds(100,100,100,20);
port.setEditable(true);
ok=new JButton("Login");
ok.setBounds(30,130,70,20);
cancel= new JButton("Cancel");
cancel.setBounds(110,130,80,20);
container.add(label1);
container.add(user);
container.add(label2);
container.add(password);
container.add(label3);
container.add(server);
container.add(label4);
container.add(port);
container.add(ok);
container.add(cancel);
user.addActionListener(this);
password.addActionListener(this);
server.addActionListener(this);
port.addActionListener(this);
ok.addActionListener(this);
cancel.addActionListener(this);
setSize(250,210);
setLocation(400,250);
}
public void actionPerformed(ActionEvent e)
{
if((e.getSource() == ok) || (e.getSource() == password)
||(e.getSource() == user) || (e.getSource() == server) ) {
Username = user.getText();
Password = new String(password.getPassword());
Server = server.getText();
if(Server.length()== 0){
JOptionPane.showMessageDialog(this,
"Invalid server host","Error",
JOptionPane.WARNING_MESSAGE);
return;
}
setVisible(false); //for OK Button of LoginDialog Class
}
else if(e.getSource() == cancel)
setVisible(false);
if(e.getSource()==ok){
//Send data over socket
String text =(ok.getText());
//create socket connection
try{
client = new Socket("localhost",4444);
} catch (IOException e1) {
System.out.println("Could not listen on port 4444");
System.exit(-1);
}
try{
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
out.println(text);
String line1 = in.readLine();
String line2 = in.readLine();
if (line1.equals(user.getText())) {
user.setText(line1);
password.setText(line2);
}
} catch (IOException e1) {
System.out.println("Accept failed: 4444");
System.exit(-1);
}
}
}
}