Click here to Skip to main content
15,886,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all im writing a chat server program in eclipse i have the chat client and chat server talking to each other and wanted to add a username and password entry to this, i have the password field working but i cant for the life of me think of how get the chat program to run after clicking the submit button, any help would be really appreciated, thanks.


Java
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
 
class Login extends JFrame implements ActionListener
{
 JButton SUBMIT;
 JPanel panel;
 JLabel label1,label2;
 final JTextField  text1,text2;
  Login()
  {
  label1 = new JLabel();
  label1.setText("Username:");
  text1 = new JTextField(15);

  label2 = new JLabel();
  label2.setText("Password:");
  text2 = new JPasswordField(15);
 
  SUBMIT=new JButton("SUBMIT");
  
  panel=new JPanel(new GridLayout(3,1));
  panel.add(label1);
  panel.add(text1);
  panel.add(label2);
  panel.add(text2);
  panel.add(SUBMIT);
  add(panel,BorderLayout.CENTER);
  SUBMIT.addActionListener(this);
  setTitle("LOGIN FORM");
  }
 public void actionPerformed(ActionEvent ae)
  {
  String value1=text1.getText();
  String value2=text2.getText();
  if (value1.equals("Anfield") && value2.equals("L1v3rp00l")) {
  Chat chat=new Chat();
  chat.setVisible(true);
  JLabel label = new JLabel("Welcome:"+value1);
  chat.getContentPane().add(label);
  }
  else{
  System.out.println("enter the valid username and password");
  JOptionPane.showMessageDialog(this,"Incorrect login or password",
  "Error",JOptionPane.ERROR_MESSAGE);
  }
}
}
 class LoginDemo
{
  public static void main(String arg[])
  {
  try
  {
  Login frame=new Login();
  frame.setSize(300,100);
  frame.setLocation(60,40);
  frame.setVisible(true);
  }
  catch(Exception e)
  {JOptionPane.showMessageDialog(null, e.getMessage());}
  }
}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 4-Mar-13 6:03am
v2
Comments
Richard MacCutchan 4-Mar-13 12:28pm    
Call the Login class at the beginning of the chat client, and only continue if the correct password is entered.

1 solution

Richards comment is short form what you've got to do:

create a function that you call before you create your GUI:

Java
public String[] doLogin(){
  //create Dialog with a Comment and 2 Textfields
  JTextField name = new JTextField();
  JPasswordField pwd = new JPasswordField(); // for password - will show only "*"
  Object[] message = { "Please enter Name and Password\n", name, pwd };
  int iResponse = JOptionPane.showConfirmDialog(null, message, "Login",  // show and wait for Dialog to be closed
      JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);

  if(iResponse== JOptionPane.OK_OPTION) { // check dialog's exit condition
      return new String[] {name.getText(), pwd.getPassword()); // returning the log in credentials 
  }
  return null; // You need to react on null as return -> end application's lifecycle
  
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900