Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
 

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 4-Mar-13 6:56am
Edited 4-Mar-13 7:03am
v2
Comments
Richard MacCutchan at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Richards comment is short form what you've got to do:
 
create a function that you call before you create your GUI:
 
 
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
  
}
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 609
1 OriginalGriff 587
2 Maciej Los 325
3 Mathew Soji 195
4 BillWoodruff 190
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 5 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100