Reign
Reign

Reputation: 289

Java applet not reprinting as it should

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();

    }
}

Upvotes: 0

Views: 55

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168845

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)

Upvotes: 1

Reimeus
Reimeus

Reputation: 159844

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

Upvotes: 1

Related Questions