Solved

Swing applet with a JTextField asking user for a password

Posted on 2003-10-21
3
725 Views
Last Modified: 2013-11-23
Hi,
I have to Create a Swing applet that asks a user to enter a password into a JTextField and to then press [Enter].
Compare the password to "Rosebud"; if it marches, display "Access Granted", if not display "Access Denied.

I tried to write a program but everytime I run it, whenever I enter the correct password or the wrong one it comes out as "access denied".

Here is my code: (Hope you can help)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JPasswordA extends JApplet implements ActionListener
{
      JLabel greeting = new JLabel("Please, enter password");
      Font bigFont = new Font("TimesRoman", Font.ITALIC, 24);
      JButton pressMe = new JButton("Press ME");

      JTextField answer = new JTextField("",10);
      FlowLayout flow = new FlowLayout();

      public void init()
      {
            greeting.setFont(bigFont);
            Container con = getContentPane();
            con.add(greeting);
            con.setLayout(flow);
            con.add(answer);
            con.add(pressMe);
            pressMe.addActionListener(this);
            answer.addActionListener(this);
            answer.requestFocus();
      }
      public void actionPerformed(ActionEvent thisEvent)
      {
            Object source = thisEvent.getSource();
                  if(source == ("Rosebud"))
                  {
                  String name = answer.getText();
                  System.out.println("Access granted");
            }
                  else if(source instanceof JTextField);
                  {
                        String name = answer.getText();
                        System.out.println("Access denied");
                  }
            }
      }
0
Comment
Question by:stdebernardi
  • 2
3 Comments
 
LVL 9

Expert Comment

by:yongsing
ID: 9596611
The following will work.

public void actionPerformed(ActionEvent thisEvent)
{
    Object source = thisEvent.getSource();
    if (source instanceof JTextField)
    {
          String name = answer.getText();
          if (name.equals("Rosebud")) {
              System.out.println("Access granted");
          }
          else
          {
              System.out.println("Access denied");
          }
    }
}

0
 
LVL 9

Accepted Solution

by:
yongsing earned 150 total points
ID: 9596646
If you want the checking to be done when either the Enter key is pressed or the button is clicked, then the following code will do:

public void actionPerformed(ActionEvent thisEvent)
{
     String name = answer.getText();
     if (name.equals("Rosebud")) {
         System.out.println("Access granted");
     }
     else
     {
         System.out.println("Access denied");
     }
}


By the way, you should use a JPasswordField instead. This component will display the characters you enter as *.
0
 

Author Comment

by:stdebernardi
ID: 9596775
That's great.
Thank you so much.

I thought about using a JPasswordField but the requirement was a JTextField, I don't know why, a JPasswordField would have been more realistic. Oh well.

Thanks again.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 71
bitbucket vs gitbucket 3 58
Arrays.asList  VS  ArrayList 4 45
hibernate jars 4 9
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now