0

This is a applet I'm busy making but it's not updating as it should. can any one pic up on where i went wrong? this is part of a course I'm taking so it has to be more or less as it is. Please keep in mind I am a beginner when it comes to java thanks guys...

import java.awt.*;
import java.applet.*;
import java.awt.event.*;


public class PasswordApplet extends Applet implements ActionListener
{
   //Declaring variables
   String id, password;
   boolean success;
   String idArray[] = {"Reign", "Test", "Others"};
   String passwordArray[] = {"130489", "1234", "1111"};


   //Create components for applet
   Label headerLabel = new Label("Please type your ID and Password");


   Label idLabel = new Label("ID:");
      TextField idField = new TextField(8);

   Label passwordLabel = new Label("Password:");
      TextField passwordField = new TextField(8);


   Button loginButton = new Button("Login");

   public void init()
   {
      //Set color, layout, and add components
      setBackground(Color.orange);

      setLayout(new FlowLayout(FlowLayout.LEFT,50,30));

      add(headerLabel);

      add(idLabel);
         add(idField);
         idField.requestFocus();

      add(passwordLabel);
         add(passwordField);
         passwordField.setEchoChar('*');

      add(loginButton);
         loginButton.addActionListener(this);

    }

   public void actionPerformed(ActionEvent e)
   {
       success = false;

      //Sequential search
      for (int i = 0; i<idArray.length; i++)
      {
   if ((idArray[i].compareTo(id)==0)&&(passwordArray[i].compareTo(password)==0))
            success=true;
      }

      if(success=true)
      {
        headerLabel.setText("Login Successful");
        headerLabel.repaint();


      }
      else
      {
        headerLabel.setText("Invalid. Try Again");
        headerLabel.repaint();

      }
        repaint();

    }
}
Reign
  • 289
  • 1
  • 9
  • You haven't stated what is supposed to update. – Paul Samsotha Apr 15 '14 at 11:16
  • 1) Please refer the teacher to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Apr 16 '14 at 08:05
  • well the only reason is that the Text book i am using states i must use the AWT unfortunately its part of the course requirements and I'm sure the individuator will not mark it if i dont follow the instructions, I am studying via correspondence. – Reign Apr 17 '14 at 09:35

2 Answers2

1

You need to assign the id field before comparing its value in the ActionListener

id = idField.getText();

otherwise an NPE will be thrown preventing any UI updates

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Thanks alot. So I put that in(it fixed the NPE exception), now any password works even a blank field. Any ideas? add(idLabel); add(idField); idField.requestFocus(); id = idField.getText() – Reign Apr 15 '14 at 11:48
1

This next statement is assigning true to the attribute success.

if(success=true)

What it actually needs is to compare the value to true. So it should be:

if(success==true)

Or better:

if(success)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks It helped now It doesnt want to log in i have tryed to return the vaues coming from the feilds with System.out.println(password);System.out.println(id); and it retuns a empty line – Reign Apr 17 '14 at 08:24
  • It would also help to answer the question asked in the 2nd point of my first comment. It was not meant as a 'rhetorical question' - I expect an answer. – Andrew Thompson Apr 17 '14 at 08:59