0

I am new to Java. I want to make a registration program which I will use later for Login. I want to get the user to input their User name (KorisnickoIme), Password (Lozinka) and Repeat password (PLozinka); compare Lozinka with PLozinka; and if all is good, add Lozinka as a value to a hashmap with key KorisnickoIme.

The problem is that when I print lozinka, I get nothing. So that means that I got nothing in the Hash map too, and I can't use it later for Login stuff.

public class RegistrujSeProzor {
    static HashMap<String, Object> korisnici = new HashMap<>();

    static String KorisnickoIme = "";
    static String Lozinka = "";
    static String PLozinka = "";

    static void prozor () {
        JFrame prozor = new JFrame();
        prozor.setLayout(null);
        prozor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        prozor.setIconImage(new ImageIcon("res/purple.png").getImage());
        prozor.setVisible(true);
        prozor.setSize(400, 300);
        prozor.setLocationRelativeTo(null);
        prozor.setResizable(false);

        Container contentPane = prozor.getContentPane();

        JLabel label1 = new JLabel("Novo Korisnicko Ime : ");
        JLabel label2 = new JLabel("Nova Lozinka : ");
        JLabel label3 = new JLabel("Ponovte Lozinku : ");

        label1.setBounds(10, 20, 200, 30);
        label2.setBounds(10, 50, 200, 30);
        label3.setBounds(10, 80, 200, 30);

        contentPane.add(label1);
        contentPane.add(label2);
        contentPane.add(label3);

        JTextField textField = new JTextField(20);
        contentPane.add(textField);
        textField.setBounds(140, 26, 200, 20);
        KorisnickoIme = textField.getText();

        JTextField textField2 = new JTextField(20);
        contentPane.add(textField2);
        textField2.setBounds(140, 57, 200, 20);
        Lozinka = textField2.getText();

        JTextField textField3 = new JTextField(20);
        contentPane.add(textField3);
        textField3.setBounds(140, 88, 200, 20);
        PLozinka = textField3.getText();

        JButton dPotvrdi = new JButton("Potvrdi");
        dPotvrdi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (Lozinka.equals(PLozinka)) {
                    korisnici.put(KorisnickoIme, Lozinka);
                    UlogujSeProzor.main(null);
                    prozor.dispose();
                    System.out.println(Lozinka);

                }else{
                    JOptionPane.showMessageDialog(null,"Pogresno uneto Novo Korisnicko ime ili Lozinka !   "
                            + "Pokusajte ponovo .");
                    RegistrujSeProzor.main(null);
                }
            }
        });

        JButton dNazad = new JButton("Nazad");
        dNazad.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                LoginRegisterProzor.main(null);
                prozor.dispose();
            }
        });

        contentPane.add(dPotvrdi);
        contentPane.add(dNazad);

        dPotvrdi.setBounds(215, 215, 150, 30);
        dNazad.setBounds(15, 215, 150, 30);
    }

    public static String getText() {
        return KorisnickoIme;
    }

    public static void setText(String text) {
        RegistrujSeProzor.KorisnickoIme = text;
    }

    public static String getText2() {
        return Lozinka;
    }

    public static void setText2(String text2) {
        RegistrujSeProzor.Lozinka = text2;
    }

    public static String getText3() {
        return PLozinka;
    }

    public static void setText3(String text3) {
        RegistrujSeProzor.PLozinka = text3;
    }

    public static void main(String[] args) {
        prozor();
    }
khelwood
  • 55,782
  • 14
  • 81
  • 108
Bojan Kolano
  • 253
  • 3
  • 19
  • 2
    1. 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). 2. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). .. – Andrew Thompson Aug 10 '16 at 14:16
  • 2
    Please read about java style guides. Variable names always start lower case; except for CONSTANTS. Then I would suggest: read a bit about "static" versus "non-static" methods/fields. You are using static all over the place, and that is simply not a good idea. Third: dont do that. Dont have multiple "things" where you do setText1, setText2, and so on. That is simply very bad practice. Use names that say what the thing behind the name **is**. – GhostCat Aug 10 '16 at 14:16
  • 2
    .. 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 4) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Aug 10 '16 at 14:17
  • @Andrew Thompson you jumped on this guy too :D. – whyn0t Aug 10 '16 at 14:22
  • Ok i get that there are some mistakes , as said , im new in Java , and i will learn naming and stuff.But as it is , I do have Some results as it is now i just need help so i can make it functional and get to Loging in. I can change names and make adjustments later. – Bojan Kolano Aug 10 '16 at 14:29
  • you dont have to make so complicated programs the first time try to remove that prozor.dispose(); before the display good luck – whyn0t Aug 10 '16 at 14:38
  • @whyn0t *"you jumped on this guy too"* Seriously, it's time for the internet to have less 'precious princesses'. If you have a problem with any of my answers or comments, feel free to report them. I'd bet that the only one that gets deleted is this one. But report them rather than bore me with the details.. O_o – Andrew Thompson Aug 10 '16 at 15:00
  • *"I can change names"* The best time to learn proper nomenclature is before the code is presented in public. After all, source code is meant to be read by people, not computers. *"..and make adjustments later."* 'Early' is also a good time to learn best practices. – Andrew Thompson Aug 10 '16 at 15:02

1 Answers1

0
Lozinka = textField2.getText();

You're reading the text field right after its creation, when it's still empty. You should do that inside your ActionListener's actionPerformed method, for example:

button.addActionListener(e -> {
    String password1 = passwordField.getText();
    String password2 = repeatPasswordField.getText();

    // compare
});

Or the standard way if you dislike the lambdas:

button.addActionListener(new ActionListener() {
   @Override    
   public void actionPerformed(ActionEvent e) {
      String password1 = passwordField.getText();
      String password2 = repeatPasswordField.getText();

      // compare    
    }
});
Dth
  • 1,916
  • 3
  • 23
  • 34