ActionPerformed method not working when applet is loaded in browser window.

Advertisement
Hey there guys. I need urgent help from anybody who has experience in deploying websites whose code is in java.
I am having two problems as mentioned below...
first, I have made a simple login screen using java swing and JApplet. there is a single button to login. the action performed for this button accesses a private method to check the username and password which are there in atext file. the applet is working perfectly in appletviewer but when i load the applet in a Internet Explorer window using HTML's Applet tag, the button is giving no response at all even when i enter the correct username and password.
I guess it is either not calling the private function that is checking the username and password from the tes=xt file or it can not access the file. Please help as soon as possible as this is related to my college project.
I am attaching the code herewith. Suggestions to improve the coding are also welcome.
the second problem is that while writing my second program for generating a form which registers a user the html is not at all loading the applet into the browser and also if im trying to access a file to write all the details into the console is showing numerous amount of error after i press the button which i can't not understand. the only thing i can understand is that it is related to file access permissions. If anybody could put some light on the working of worker threads and thread safe activities of SwingUtilities.invokeandWait method it would be really appreciable.
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
<applet code = "UserLogin" width = 300 height = 150>
</applet>
public class UserLogin extends JApplet implements ActionListener, KeyListener {
     private JLabel lTitle;
     private JLabel lUsername, lPassword;
     private JTextField tUsername;
     private JPasswordField tPassword;
     private JButton bLogin;
     private JLabel lLinkRegister, lLinkForgot;
     private JLabel lEmpty = new JLabel(" ", JLabel.CENTER);
     private JPanel panel1, panel2;
     public void init() {
          try {
               UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
               SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                         LoginGUI();
          catch(Exception e) {
               e.printStackTrace();
     public void start() {
          setVisible(true);
     public void stop() {
          setVisible(false);
     private void LoginGUI() {
          super.setSize(300, 150);
          super.setBackground(Color.white);
          lTitle = new JLabel("<HTML><BODY><FONT FACE = \"COURIER NEW\" SIZE = 6 COLOR = BLUE>Login</FONT></BODY></HTML>", JLabel.CENTER);
          lUsername = new JLabel("Username : ", JLabel.CENTER);
          lPassword = new JLabel("Password : ", JLabel.CENTER);
          tUsername = new JTextField(15);
          tPassword = new JPasswordField(15);
          bLogin = new JButton("LOGIN");
//          bLogin.setEnabled(false);
          bLogin.addActionListener(this);
          bLogin.addKeyListener(this);
          panel2 = new JPanel();
          GridBagLayout gbag = new GridBagLayout();
          GridBagConstraints gbc = new GridBagConstraints();
          panel2.setLayout(gbag);
          panel2.addKeyListener(this);
          gbc.anchor = GridBagConstraints.CENTER;
          panel2.setMinimumSize(new Dimension(300, 200));
          panel2.setMaximumSize(panel2.getMinimumSize());
          panel2.setPreferredSize(panel2.getMinimumSize());
          gbc.gridx = 1;
          gbc.gridy = 1;
          gbag.setConstraints(lUsername,gbc);
          panel2.add(lUsername);
          gbc.gridx = 2;
          gbc.gridy = 1;
          gbag.setConstraints(tUsername,gbc);
          panel2.add(tUsername);
          gbc.gridx = 1;
          gbc.gridy = 2;
          gbag.setConstraints(lPassword,gbc);
          panel2.add(lPassword);
          gbc.gridx = 2;
          gbc.gridy = 2;
          gbag.setConstraints(tPassword,gbc);
          panel2.add(tPassword);
          gbc.gridx = 2;
          gbc.gridy = 3;
          gbag.setConstraints(lEmpty,gbc);
          panel2.add(lEmpty);
          gbc.gridx = 2;
          gbc.gridy = 4;
          gbag.setConstraints(bLogin,gbc);
          panel2.add(bLogin);
          panel1 = new JPanel(new BorderLayout());
          panel1.add(lTitle, BorderLayout.NORTH);
          panel1.add(panel2, BorderLayout.CENTER);
          add(panel1);
          setVisible(true);
     public void keyReleased(KeyEvent ke) {}
     public void keyTyped(KeyEvent ke) {}
     public void keyPressed(KeyEvent ke) {
          if(ke.getKeyCode() == KeyEvent.VK_ENTER){
               String username = tUsername.getText();
               String password = new String(tPassword.getPassword());
               if(username.length() == 0 || password.length() == 0) {
                    JOptionPane.showMessageDialog(new JFrame(),"You must enter a username and password to login", "Error", JOptionPane.ERROR_MESSAGE);
               else {
                    boolean flag = checkUsernamePassword(username, password);
                    if(flag)
                         JOptionPane.showMessageDialog(new JFrame(),"Username and Password Accepted", "Access Granted", JOptionPane.INFORMATION_MESSAGE);
                    else
                         JOptionPane.showMessageDialog(new JFrame(),"Username or password Incorrect", "Access Denied", JOptionPane.INFORMATION_MESSAGE);
     public void actionPerformed(ActionEvent ae) {
          String gotCommand = ae.getActionCommand();
          if(gotCommand.equals("LOGIN")) {
               String username = tUsername.getText();
               String password = new String(tPassword.getPassword());
               if(username.length() == 0 || password.length() == 0) {
                    JOptionPane.showMessageDialog(new JFrame(),"You must enter a username and password to login", "Error", JOptionPane.ERROR_MESSAGE);
               else {
                    boolean flag = checkUsernamePassword(username, password);
                    if(flag)
                         JOptionPane.showMessageDialog(new JFrame(),"Username and Password Accepted", "Access Granted", JOptionPane.INFORMATION_MESSAGE);
                    else
                         JOptionPane.showMessageDialog(new JFrame(),"Username or password Incorrect", "Access Denied", JOptionPane.INFORMATION_MESSAGE);
     private boolean checkUsernamePassword(String username, String password) {
          String user = null, pswd = null;
          try {
               FileInputStream fin = new FileInputStream("@data\\userpass.txt");
               DataInputStream din = new DataInputStream(fin);
               BufferedReader brin = new BufferedReader(new InputStreamReader(din));
               user = (String) brin.readLine();
               pswd = (String) brin.readLine();
          catch(IOException ioe) {
               ioe.printStackTrace();
          if(username.equals(user) && password.equals(pswd))
               return true;
          else
               return false;
}PLEASE HELP ME GUYS......
Advertisement

Replay

RockAsh wrote:
Hey Andrew, first of all sorry for that shout, it was un-intentional as i am new to posting topics on forums and didn't new that this kind of writing is meant as shouting. Cool.
Secondly thank you for taking interest in my concern.No worries.
Thirdly, as i mentioned before, I am reading i file for checking of username and password. the file is named as "userpass.txt" and is saved in the directory named "@data" which is kept in the same directory in which my class file resides.OK - server-side. That makes some sense, and makes things easier. The problem with your current code is that the applet will be looking for that directory on the end user's local file system. Of course the file does not exist there, so the applet will fail unless the the end user is using the same machine as the server is coming from.
To get access to a resource on the server - the place the applet lives - requires an URL. In applets, URLs are relatively easy to form. It might be something along the lines of
URL urlToPswrd = new URL(getCodeBase(), "@data/userpass.txt");
InputStream is = urlToPswrd.openStream();
DataInputStream din = new DataInputStream(is);
So the problem is that it is reading the file and showing the specific output dialog box when i run it through appletviewer.. Huhh. What version of the SDK are you using? More recent applet viewers should report security exceptions if the File exists.
..but the same is not happening when i launch the applet in my browser window using the code as written belowHave you discovered how to open the Java Console in the browser yet? It is important.
Also the answer to your second question
Also, the entire approach to storing/restoring the password is potentially wrong. For instance, where is it supposed to be stored, on the server, or on the client?is that, as of now it is just my college project so all the data files and the username and password wiles will be stored on my laptop only i.e. on the client only. no server involved.OK, but understand that an applet ultimately does not make much sense unless deployed through a server. And the entire server/client distinction becomes very important, since that code would be searching for a non-existent file on the computer of the end user.

Fscommand is not working when opening PDF document in browser with Acrobat 9, why?

We embedded a flash application in PDF using screen annotation. In the flash we use fscommand to call methods available in AcroJS.  In acrobat reader 9, if we view the document in Internet Explorer, we receive a security sandbox violation message say

Movie clip resize not working when image is loaded

Hello, I'm having some strange luck in building an image slide show. I load the image paths into an array from an XML page and then step through the array elements w/ forward and back buttons. I have an empty image clip on the stage where I create an

Keyboard backlighting not working when using Windows in Boot Camp

Hi, I'm posting this for a friend with a new Macbook Air. He says that his keyboard backlighting does not work when he is using it for Windows (don't know which, but probably Vista). Any ideas why? Thanks. GaryInstall all the drivers needed when runn

HT1430 For some unknown reason my Apple ID password does not work when trying to down load books from the IBook or Nook Apps.  It also has stop work when trying to down load new Apps.  Any suggestions out their???

For some unknown reason, my Apple ID Password does not work when trying to down load IBooks, Nook books, or new Apps.  Everything else seems to work.  Any suggestions out their???I appreciate the info and realize that the Nook App is not related to m

Css3 animation does not work when triggerd by backing bean method

Hi, I use Jdev 11.1.1.4 (but I guess the Jdev/ADF version does not matter in this case) and Firefox 15.0.1. I tested some parts of article https://blogs.oracle.com/groundside/entry/css_animations_the_panel_flip from Duncan Mills. The CSS3 animation f

HT203254 my desktop is not working when i open it all it does is show the color weal and it never gos away on my mac book pro how do i fix it?

my desktop is not working when i open it all it does is show the color weal and it never gos away on my mac book pro how do i fix it?It means, tentatively, that system files are missing or damaged and you will have to reinstall Snow Leopard. See the

Report with JDBC connection does not work when they includes CommandTable

I am trying to render using the new version of Crystal report java component - CRJ a report contains Data base Fileds of type Command Table (Row Set) which seems to be not working. when i use the previes version of crystal SDK it works fine. after de

Sound not working when watching movies from MacBook Pro 15'' in full screen

after a recent iTunes upgrade the sound will not work when I watch a movie or anything else in iTunes on my Thunderbold Display. I have the 15'' retina MacBook Pro connected to the display and it used to work flawlesly. has anyone had the same proble

Iphone 4 the mike does not work when making a call.  it does work with speaker phone and face time

Just activated new Iphone 4 and the mike does not work when making a call.  it does work with speaker phone and face time. Any known fixes?My wife's iphone 4 also has your same no ring problem as does my iphone 5 just less often.  I called her iphone

Dbx does not work when debugging g++ exes.

Hi, We are just getting started building our application on redhat el5 with sunstudio 12. We are compiling and linking our app with gcc 4.1 as we need to be able to link against some 3rd party libraries build on rhel4 that we do not have source for.

My iPad 2 charger is not working when plugged to a wall..but when plugged to a computer...it says unknown device...what's happening and how do I fix this problem?!

My iPad 2 charger is not working when plugged to a wall..but when plugged to a computer...it says unknown device...what's happening and how do I fix this problem?!Have you recharge your iPod for at least 2 hours, preferably till the Full charge Batte

Our online number is not working when trying to ca...

Hej Our online number is not working when trying to call from Telia mobile phones in denmark. I have checked with Telia in Denmark and they tell us that the problem is a "prefix" that is set wrong from SKYPE's side? Could someone at skype please

Exchange Impersonation not working when applied to group or OU

I am trying to set up Exchange Impersonation so a specific account can impersonate a group of users. I have successfully set it up and tested to impersonate a single user but it will not work when I apply it to either a distribution group or OU, even

SP2013: OK button not working when editing Settings on List app

Hi, all!  Currently running Sharepoint 2013 under Server 2008 R2.  My client is Win7 Pro/64-bit running lastest Firefox, Chrome & IE11.  I am currently a Domain Admin for our corp domain. Long story short, I've created a test List app.  When I attemp

I inherited an 2003 ipod from my daughter. The controls will not work when it is plugged into a power source. Can anyone help?

I inherited a 2003 ipod from my daughter. The controls will not work when it is plugged into a power source. Can anyone help?But they work normally when not plugged in?  What kind of power source are you plugging it into? It maybe outputting more tha

Just upgraded to Lion an am discovering that I cannot send email photos from within iPhoto. Error message says bad Internet connection or server not working, when that's not the case. Never happened in Snow Leopard! Help!!!

Just upgraded to Lion an am discovering that I cannot send email photos from within iPhoto. Error message says bad Internet connection or server not working, when that's not the case. Never happened in Snow Leopard! Help!!!what email service - Yahoo

Why is my touch screen function not working when using facetime

Why is my touch screen function not working when using facetimeHello megascones, After reviewing your post, it sounds like the screen is not responding to touch in one app. I would recommend that you read this article, it may be able to help the issu

Auto Lock not working when connected to Macbook

Ive had my ipod for about a week and I thought auto-lock was working even when the ipod is connected to my computer. But I just realized that the auto-lock is not working when it is connected to the computer. Is this normal? Also when I set the brigh

I bought my iphone 5s a week before and now its volume "up" button is not working when pressed.Does anybody know what to do to make it work?I have a one year warranty.Should i give it to apple retail shop or is there any way to solve this problem?

I bought my iphone 5s a week before and now its volume "up" button is not working when pressed.Does anybody know what to do to make it work?I have a one year warranty.Should i give it to apple retail shop or is there any way to solve this proble