-1

I have two frames. One is for login_page (where user enters email and password). Here the authentication is done using google API. In the other frame (which has few comboboxes and radio buttons to be selected), but for one radio button I want a JOptionPane to pop up with a password and remember me option as a checkbox, only if the password is correct I can allow him to submit.

How can I store the password using remember me as checkbox?

Anthon
  • 69,918
  • 32
  • 186
  • 246
avaj
  • 3
  • 1
  • 3
  • 1
    The question is very broad, which aspect are you having trouble with... – MadProgrammer Mar 08 '15 at 10:05
  • I just want a JOptionPane with password and a checkbox(Remember me), when password is entered and checkbox is selected, i need to store the password... – avaj Mar 08 '15 at 10:09
  • for this simple situation you can just store you password into a File and the Retrieve it again with a Scanner. – crAlexander Mar 08 '15 at 12:49
  • Store a salted hash, as suggested in this possible [duplicate](http://stackoverflow.com/q/27213331/230513). – trashgod Mar 08 '15 at 13:08

2 Answers2

1
  • Chapter 1:

What is a data base:A large collection of associated data

For big apps this way just don't exist there you have to use dataBases like sql,oracle..etc some open source like mysql,miniBase..etc

  • Chapter 2:

I wrote a simple app for your needs.It does the thinks you descripted and saving the name and the password into a File[on user Desktop].

enter image description here

The Class:

 import java.awt.FlowLayout;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.util.Scanner;

 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JPasswordField;
 import javax.swing.JRadioButton;
 import javax.swing.JTextField;

public class Applic {

//This will be the file where the username and password will be saved
File file = new File(System.getProperty("user.home")+"/Desktop/save.txt");


//Window,buttons etc...
JFrame frame= new JFrame();
JTextField name = new JTextField(20);
JPasswordField password = new JPasswordField(20);
JRadioButton remember = new JRadioButton("Remember me");
JButton Enter = new JButton("Enter");

   public Applic(){


     //Create the Window
       frame.setSize(250,200);
       frame.setLocationRelativeTo(null);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setResizable(false);
       frame.setLayout(new FlowLayout());

       //Add The items to window
       frame.add(name);
       frame.add(password);
       frame.add(remember);
       frame.add(Enter);


       UPDATE(); //Check if password and this User is Saved (very simple for one user)
       frame.setVisible(true);


       //Add A mouseAdapter(or whatever you want
       Enter.addMouseListener(new MouseAdapter(){
           public void mouseReleased(MouseEvent m){

               if(remember.isSelected()){
                  SAVE(); //Save This UserName and his PassWord     
               }

           }//end of mouseReleased
       });
   }


public static void main(String[] args){
    new Applic();
}


  public void SAVE(){      //Save the UserName and Password (for one user)



        try {
            if(!file.exists()) file.createNewFile();  //if the file !exist create a new one

            BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsolutePath()));
            bw.write(name.getText()); //write the name
            bw.newLine(); //leave a new Line
            bw.write(password.getPassword()); //write the password
            bw.close(); //close the BufferdWriter

        } catch (IOException e) { e.printStackTrace(); }        

 }//End Of Save




  public void UPDATE(){ //UPDATE ON OPENING THE APPLICATION

        try {
          if(file.exists()){    //if this file exists

            Scanner scan = new Scanner(file);   //Use Scanner to read the File

            name.setText(scan.nextLine());  //append the text to name field
            password.setText(scan.nextLine()); //append the text to password field
            scan.close();
          }

        } catch (FileNotFoundException e) {         
            e.printStackTrace();
        }                

   }//End OF UPDATE



}//End Of Class [Applic]
crAlexander
  • 376
  • 1
  • 2
  • 12
0

If you use database than keep checkbox option in database. Otherweise, If checkbox is selected than store your password in some hidden file at user path. Next time before login look at that password.

Edit: Sample code for file based rememberme password or username

    String path=System.getProperty("user.home")+"/.myapp";
    FileWriter file=new FileWriter(path);
    file.write("user_password");
    file.close();
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • am not using database... i just want a simple code, when password is entered and checkbox is selected, that passowrd should be stored – avaj Mar 08 '15 at 10:03
  • You can use `PrintWriter` or `FileWriter` class. – Masudul Mar 08 '15 at 10:05