Hi! I am just a newbie here! I just wanna ask how to do the file handling in java applet? Do I have to do the certain policy/permission to perform it? If so, how would I do it?
And another one, how can I close the APPLET WINDOW once I click on the exit button. I was able to close the Frame but I can't close the APPLET WINDOW.
can you help me guys?

codes for my program. this is what i have actually. i just started doing this one hmm, 4hrs ago? i've just started with the components that i am going to use:

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

public class CaseStudy extends Applet implements ActionListener, ItemListener
{ Label jlbl1 = new Label("Username:");
  Label jlbl2 = new Label("Password:");
  Label lbl1 = new Label("Shoe Inventory!");
  Label lbl2 = new Label("What would you like to do?");
  Label lbl3 = new Label("Type-in the stock number:");
  Label lbl4 = new Label("How many pairs?");
  Label lbl5 = new Label("Enter the price:");
  Label lbl6 = new Label("Others pls. specify:");//size

  TextField txt1 = new TextField("",20);
  TextField txt2 = new TextField("",20);
  TextField txt3 = new TextField("",25); //stock number
  TextField txt4 = new TextField("",20); //no. of pairs
  TextField txt5 = new TextField("",20); //price
  TextField txt6 = new TextField("",20); //size

  JButton jb1 = new JButton("Confirm");
  JButton jb2 = new JButton("Clear Fields");
  JButton jb3 = new JButton("Create");
  JButton jb4 = new JButton("Edit");
  JButton jb5 = new JButton("Delete");
  JButton jb6 = new JButton("Display");
  JButton jb7 = new JButton("Search");
  JButton jb8 = new JButton("Exit");
  JButton jb9 = new JButton("<<Back"); //buttons for file handling
  JButton jb9_1 = new JButton("<<Back"); //frame1 create
  JButton jb9_2 = new JButton("<<Back"); //frame2 edit
  JButton jb9_3 = new JButton("<<Back"); //frame3

  Choice chc = new Choice(); //submit and cancel

  CheckboxGroup chkb = new CheckboxGroup(); //brand
  Checkbox nike = new Checkbox("Nike", chkb, false);
  Checkbox adi = new Checkbox("Adidas", chkb, false);
  Checkbox n1 = new Checkbox("And1", chkb, false);
  Checkbox ske = new Checkbox("Skechers", chkb, false);
  Checkbox chk = new Checkbox("Brand");

  CheckboxGroup chkb2 = new CheckboxGroup(); //color
  Checkbox blu = new Checkbox("Blue", chkb2, false);
  Checkbox yel = new Checkbox("Yellow", chkb2, false);
  Checkbox pin = new Checkbox("Pink", chkb2, false);
  Checkbox bla = new Checkbox("Black", chkb2, false);
  Checkbox chk2 = new Checkbox("Color");

  CheckboxGroup chkb3 = new CheckboxGroup(); //style
  Checkbox high = new Checkbox("High Cut", chkb3, false);
  Checkbox low = new Checkbox("Low Cut", chkb3, false);
  Checkbox chk3 = new Checkbox("Style");

  CheckboxGroup chkb4 = new CheckboxGroup(); //size
  Checkbox s6 = new Checkbox("6", chkb4, false);
  Checkbox s7 = new Checkbox("7", chkb4, false);
  Checkbox s8 = new Checkbox("8", chkb4, false);
  Checkbox s9 = new Checkbox("9", chkb4, false);
  Checkbox s10 = new Checkbox("10", chkb4, false);
  Checkbox chk4 = new Checkbox("Size");

  Panel p1 = new Panel();
  Panel p2 = new Panel();
  Panel p3 = new Panel();
  Panel p4 = new Panel();
  Panel p5 = new Panel();
  Panel p6 = new Panel();
  Panel p7 = new Panel();
  Panel p8 = new Panel();
  Panel p9 = new Panel();
  Panel p10 = new Panel();
  Panel p11 = new Panel();
  Panel p12 = new Panel();
  Panel p13 = new Panel();
  Panel p14=new Panel();

  GridLayout gl = new GridLayout(2,2);
  GridLayout gl1 = new GridLayout(2,1);
  GridLayout gl2 = new GridLayout(6,1);
  GridLayout gl3 = new GridLayout(6,6);
  FlowLayout fl = new FlowLayout();
  GridLayout cgl=new GridLayout(8,1);

  Font f = new Font("Font.Monospaced", Font.BOLD, 15);
  Font f2 = new Font("Font.Monospaced", Font.BOLD, 25);
  Font f3 = new Font("Font.Monospaced", Font.BOLD, 17);

  JFrame fra = new JFrame(); // file handling
  JFrame fra1 = new JFrame(); //create
  JFrame fra2 = new JFrame(); //edit
  JFrame fra3 = new JFrame(); //delete

  Image img;
  String a = "java4.jpg";

  AudioClip audioClip;


  public void init()
  { img = getImage(getDocumentBase(), a);
    setSize(500, 400);

    lbl1.setFont(f2);
    lbl1.setForeground(Color.green);
    lbl1.setBackground(Color.black);
    add(lbl1);

    jlbl1.setForeground(Color.green);
    jlbl1.setFont(f);
    p1.add(jlbl1);
    p1.add(txt1);
    jlbl2.setFont(f);
    jlbl2.setForeground(Color.green);
    p1.add(jlbl2);
    txt2.setEchoChar('*');
    p1.add(txt2);
    p1.setBackground(Color.black);
    p1.setLayout(gl);

    p2.setBackground(Color.black);
    jb1.setFont(f);
    p2.add(jb1);
    jb2.setFont(f);
    p2.add(jb2);

    p3.add(p1);
    p3.add(p2);
    p3.setLayout(gl1);
    add(p3);

    jb1.addActionListener(this);
    jb2.addActionListener(this);

    fra.setLayout(gl2); //frame 1
    fra.setTitle("File Handling");
    p4.add(jb3);
    p4.add(jb4);
    p4.add(jb5);
    p4.add(jb6);
    p4.add(jb7);
    p4.add(jb8);
    p4.add(jb9);
    add(p4);
    p4.setFont(f2);

    fra.add(p4);
    jb3.addActionListener(this);
    jb4.addActionListener(this);
    jb5.addActionListener(this);
    jb6.addActionListener(this);
    jb7.addActionListener(this);
    jb8.addActionListener(this);
    jb9.addActionListener(this);

    fra1.setLayout(gl2); //frame for create
    fra1.setTitle("Informations");
    fra1.setSize(450,500);
    p13.setFont(f);
    p13.add(lbl3);  // stock number
    p13.add(txt3);

    chk.setFont(f3);
    p12.add(chk);
    nike.setEnabled(false); //brand
    adi.setEnabled(false);
    n1.setEnabled(false);
    ske.setEnabled(false);
    p12.add(nike);
    p12.add(adi);
    p12.add(n1);
    p12.add(ske);

    chk2.setFont(f3);
    p11.add(chk2);//color
    blu.setEnabled(false);
    yel.setEnabled(false);
    pin.setEnabled(false);
    bla.setEnabled(false);
    p11.add(blu);
    p11.add(yel);
    p11.add(pin);
    p11.add(bla);

    chk3.setFont(f3);
    p10.add(chk3); //style
    p10.setLayout(fl);
    high.setEnabled(false);
    low.setEnabled(false);
    p10.add(high);
    p10.add(low);

    chk4.setFont(f3);
    p9.add(chk4); //size
    s6.setEnabled(false);
    s7.setEnabled(false);
    s8.setEnabled(false);
    s9.setEnabled(false);
    s10.setEnabled(false);
    p9.add(s6);
    p9.add(s7);
    p9.add(s8);
    p9.add(s9);
    p9.add(s10);
    p9.add(lbl6);
    p9.add(txt6);

    lbl4.setFont(f3);
    p8.add(lbl4); //no. of pairs
    p8.add(txt4);

    lbl5.setFont(f3);

    p7.add(lbl5); //price
    p7.add(txt5);

    p6.add(lbl2);//what would you like to do
    chc.add("Submit");
    chc.add("Cancel");
    add(chc);
    add(p6);
    p6.add(chc);
    p6.add(jb9_1);

    chk.addItemListener(this);
    chk2.addItemListener(this);
    chk3.addItemListener(this);
    chk4.addItemListener(this);
    jb9_1.addActionListener(this);

    fra2.setLayout(gl2); //frame for edit
    fra2.setLayout(fl);
    fra2.add(jb9_2);

    jb9_2.addActionListener(this);

    fra3.setLayout(gl3); //frame for delete
    fra3.setLayout(fl);
    fra3.add(jb9_3);

    jb9_3.addActionListener(this);

    fra1.setLayout(cgl);

    fra1.add(p13);
    fra1.add(p12);
    fra1.add(p11);
    fra1.add(p10);
    fra1.add(p9);
    fra1.add(p8);
    fra1.add(p7);
    fra1.add(p6);

    audioClip = getAudioClip(getCodeBase(), "button-3.wav");

    }//init

    public void paint(Graphics g)
    {
    super.paint(g);
    g.drawImage(img,0,0,getWidth(),getHeight(),this);
    }//paint


    public void actionPerformed(ActionEvent x)
      {  Object source = x.getSource();
          if(source == jb1)
              {   audioClip.play();
                  if(txt1.getText().equals("q") && txt2.getText().equals("a"))
                  {JOptionPane.showMessageDialog(null,"Welcome " + txt1.getText() + " to Shoe Inventory Sales", "Shoe Inventory Log-in", JOptionPane.INFORMATION_MESSAGE);
                  fra.setSize(600, 500);
                  fra.setVisible(true);
                  audioClip.play();
                  txt1.setText("");
                  txt2.setText("");
                        }//if confirm correct
              else
                    { JOptionPane.showMessageDialog(null, "Username and Password not found!", "Shoe Inventory Log-in", JOptionPane.ERROR_MESSAGE);
                      txt1.setText("");
                      txt2.setText("");
                      fra.setVisible(false);
                      audioClip.play();
                      }//if confirm both wrong
                }//confirm
              if(source == jb2)
                      { audioClip.play();
                        txt1.setText("");
                        txt2.setText("");
                      }//clr
              if(source == jb3)
                    {  audioClip.play();
                       fra1.setSize(650, 350);
                       fra1.setVisible(true);
                  }//jb3
              if(source == jb4)
                    { audioClip.play();
                      fra2.setSize(600, 500);
                      fra2.setVisible(true);
                  }//jb4
              if(source == jb5)
                    { audioClip.play();
                      fra3.setSize(600, 500);
                      fra3.setVisible(true);
                    }//jb5
              if(source == jb6)
                  {
                    }//jb6
              if(source == jb7)
                    {
                    }//jb7
              if(source == jb8)
                    { audioClip.play();
                      fra.dispose();
                      stop();
                    }//jb8
              if(source == jb9)
                    { audioClip.play();
                      fra.setVisible(false);
                    }//jb9
              if(source == jb9_1)
                    { audioClip.play();
                      fra1.setVisible(false);
                    }//jb9_1
              if(source == jb9_2)
                    { audioClip.play();
                      fra2.setVisible(false);
                    }//jb9_2
              if(source == jb9_3)
                    { audioClip.play();
                      fra3.setVisible(false);
                    }//jb9_3

        }//action

    public void itemStateChanged(ItemEvent e)
            { Object y = e.getItemSelectable();
              boolean s;
                if(y == chk)
                    { audioClip.play();
                      nike.setEnabled(true);
                      adi.setEnabled(true);
                      n1.setEnabled(true);
                      ske.setEnabled(true);

                                      s = chk.getState();
                                      if(s == false)
                                        { audioClip.play();
                                          nike.setEnabled(false);
                                          adi.setEnabled(false);
                                          n1.setEnabled(false);
                                          ske.setEnabled(false);
                }//getState
                    }//chk brand

                if(y == chk2)
                    { audioClip.play();
                      blu.setEnabled(true);
                      yel.setEnabled(true);
                      pin.setEnabled(true);
                      bla.setEnabled(true);

                                      s = chk2.getState();
                                      if(s == false)
                                      { audioClip.play();
                                        blu.setEnabled(false);
                                        yel.setEnabled(false);
                                        pin.setEnabled(false);
                                        bla.setEnabled(false);
                                  }//getState chk2
                    }//chk color
                if(y == chk3)
                    { audioClip.play();
                      high.setEnabled(true);
                      low.setEnabled(true);

                                      s = chk3.getState();
                                      if(s == false)
                                          { audioClip.play();
                                            high.setEnabled(false);
                                            low.setEnabled(false);
                                      }//getState chk3
                    }//chk style
                if(y == chk4)
                    { audioClip.play();
                      s6.setEnabled(true);
                      s7.setEnabled(true);
                      s8.setEnabled(true);
                      s9.setEnabled(true);
                    }//chk size
        }//item

    public void stop()
        { System.out.print("Stoppin..");
        }
}//main

thanks in advance! :)

Edited 3 Years Ago by Dani: Formatting fixed

how to do the file handling in java applet? Do I have to do the certain policy/permission to perform it

Applets can only handle files from where they were loaded. If you load an applet from a website, it can only request files from the server it was loaded from.
The applets loaded from a web site would need permission to read files from the local PC's disk.

I can't close the APPLET WINDOW.

The applet's window belongs to the browser not the applet. You could override the paint method and fill the applets space with something. Or perhaps use the showDocument method to ask the browser to show another web page.

BTW Please use code tags (icon above to right) around your code to preserve formatting.

Edited 6 Years Ago by NormR1: n/a

NormR1 (thank you for responding :) )
i'm just using textpad, to run and to compile my program. I am not that familiar with file handling in applet actually. My friend said that I should create 'this' policy file so that I could make the applet to write files in notepad. Do i really have to do it? coz my other classmate did that policy file on his program. But I don't have an idea if his program writes a file in notepad.

Edited 6 Years Ago by newbie_java: n/a

Why do you want to handle files from an applet? It is possible but it requires all users of the applet to grant permission. Yes you can give your applet permission on your computer to handle files by adding entries to the .java.policy file using the policytool that comes with the JDK to edit the .java.policy file.

Edited 6 Years Ago by NormR1: n/a

Because that's what our professor had told us to do. She wants us to handle files from an applet. And the thing is, our prof didn't explain the whole thing about file handling in applet, that's why i am a bit crazy when it comes with this one.

Aww. so i really need to have this permission so that i could do the file handling? aww. shoot.

btw, thanks normr1 for the help! :) I guess I should better read about that policy-permission-grant thingy in applet.

If your applet reads/writes files from its home directory, I don't think you need to give it permission. For the first version of your code, get that to work and then when you figure out how to grant permission, you can expand where the applet handles files.

The tutorial has pages describing how to grant permissions.
Come back if you have more questions after you've read up on it.

Yeah sure. Thanks for your help! So glad that you've responded that fast!

NormR1 I have a question, is this the policy file that you're talking about??

// Standard extensions get all permissions by default
grant codeBase "file:${java.home}/lib/ext/*" {
        permission java.security.AllPermission;
};
// default permissions granted to all domains
grant {
        // Allows any thread to stop itself using the java.lang.Thread.stop()
        // method that takes no argument.
        // Note that this permission is granted by default only to remain
        // backwards compatible.
        // It is strongly recommended that you either remove this permission
        // from this policy file or further restrict it to code sources
        // that you specify, because Thread.stop() is potentially unsafe.
        // See "http://java.sun.com/notes" for more information.
        // permission java.lang.RuntimePermission "stopThread";

        // allows anyone to listen on un-privileged ports
        permission java.net.SocketPermission "localhost:1024-", "listen";

        // "standard" properties that can be read by anyone

        permission java.util.PropertyPermission "java.version", "read";
        permission java.util.PropertyPermission "java.vendor", "read";
        permission java.util.PropertyPermission "java.vendor.url", "read";
        permission java.util.PropertyPermission "java.class.version", "read";
        permission java.util.PropertyPermission "os.name", "read";
        permission java.util.PropertyPermission "os.version", "read";
        permission java.util.PropertyPermission "os.arch", "read";
        permission java.util.PropertyPermission "file.separator", "read";
        permission java.util.PropertyPermission "path.separator", "read";
        permission java.util.PropertyPermission "line.separator", "read";

        permission java.util.PropertyPermission "java.specification.version", "read";
        permission java.util.PropertyPermission "java.specification.vendor", "read";
        permission java.util.PropertyPermission "java.specification.name", "read";

        permission java.util.PropertyPermission "java.vm.specification.version","read";
        permission java.util.PropertyPermission "java.vm.specification.vendor","read";
        permission java.util.PropertyPermission "java.vm.specification.name", "read";
        permission java.util.PropertyPermission "java.vm.version", "read";
        permission java.util.PropertyPermission "java.vm.vendor", "read";
        permission java.util.PropertyPermission "java.vm.name", "read";
       };

--i found this on the internet. am not sure if this was the one you were referring to.

Edited 3 Years Ago by Dani: Formatting fixed

Yes that looks like a .java.policy file. Each PC can have their own tailored to what they want to allow a java applet to do.
The policytool from the JDK bin folder will handle editing a .java.policy file. The file must be in the location where the JVM looks for it. On my system the path is:
C:\Documents and Settings\Owner\.java.policy

I think I saw the policy tool. Is it an exe file?
It has this, add policy entry, edit policy entry and remove policy entry?

And I saw this one also, http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/data.html#applet

FILE ACCESS BY APPLETS
the part wherein _clickMeMode was called. I really don't know the purpose of that "_clickMeMode". and when I tried to run it on textpad, it gave me errors. like the, if (_clickMeMode), cannot find symbol File and etc. I dunno how to debug this one.

And also, it has instructions on how to create a policy file. Where will I put that policy file, the grant part? Should I have it on my current program or should I create a new class wherein I could create that policy file???

The .java.policy file is edited by the policytool program you found.
The contents of the .java.policy file define permissions for applets that execute on your PC. The JVM reads the policy file and uses the permissions defined there to control an applet.
Your classes don't read/write the policy file, use the policytool for that.

This article has been dead for over six months. Start a new discussion instead.