0

I want to make a user/pass an a checkButton to keep the user and pass. So, first time when I open the app and put the pass and user and select the rememberBox to save this check and after I will open again to remain check and the pass and user to be written already.

This is the code:

JButton btnLogin = new JButton("Login");
Image ok = new ImageIcon(this.getClass().getResource("/Ok.png")).getImage();
btnLogin.setIcon(new ImageIcon(ok));
btnLogin.addActionListener(new ActionListener() {

    @SuppressWarnings("deprecation")

    public void actionPerformed(ActionEvent arg0) {
        String user = "marius";
        String password = "Amina1";  
        UPDATE();

        if(userName.getText().equals(user) && passwordField.getText().equals(password)){
             if(checkBox.isSelected()){
                  SAVE(); //Save This UserName and his PassWord     
               }

            window.frmUserpass.dispose();
            WelcomePage welcomePage = new WelcomePage();
            welcomePage.Screen1();
        }else {
            JOptionPane.showMessageDialog(btnLogin, "Introduceti unuser si o parola valida!", "Error", 0);

            passwordField.selectAll();
        }
    }

    private void SAVE() {
        try {
            if(!file.exists()) file.createNewFile();  //if the file !exist create a new one
            BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsolutePath()));
            bw.write(userName.getText()); //write the name
            bw.newLine(); //leave a new Line
            bw.write(passwordField.getPassword()); //write the password
            bw.close(); //close the BufferdWriter
        } catch (IOException e) { e.printStackTrace(); }    
    }

    private void UPDATE() {
         try {
              if(file.exists()){    //if this file exists
                Scanner scan = new Scanner(file);   //Use Scanner to read the File
                userName.setText(scan.nextLine());  //append the text to name field
                passwordField.setText(scan.nextLine()); //append the text to password field
                scan.close();
              }
            } catch (FileNotFoundException e) {         
                e.printStackTrace();
            }                
    }   
    });
btnLogin.setBounds(231, 112, 89, 23);
frmUserpass.getContentPane().add(btnLogin);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 4
    What is your question exactly ? – Arnaud Mar 03 '17 at 13:54
  • 2
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. 3) – Andrew Thompson Mar 03 '17 at 13:57
  • 2
    .. 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 4) For security reasons, it is dangerous to store passwords. It takes special handling to write or read them with greater security. 5) There are probably a dozen ways to serialize data. Really 'too broad' for an SO answer. 6) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Mar 03 '17 at 13:57

0 Answers0