CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Swing Applet

  1. #1
    Join Date
    Oct 1999
    Location
    Maharashtra,India.
    Posts
    1

    Swing Applet

    Hello,
    I created a simple applet in swing and it run fine in my swing
    enabled browser.But when i tried something on reading and writing
    to a local file through an applet, it didn't work.I have also
    created a ploicy file with policytool.The applet is working well in the appletviewer.But when i tried to run it through my browser ,it didn't work.Where should i place my policy file?I am using<pre> <object> </object><pre> tag to load the applet in IE5.There is a text field and button on my applet,which when clicked on entering text in textbox will store it in local file and echo the same to me.
    It only loads the applet and stop functioning.If anyone can please
    help me or redirect me to some site where i will get some help i will be very grateful to you.THE JAVA FILE IS AS BELOW.

    import java.awt.Color;
    import java.awt.BorderLayout;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.applet.Applet;

    public class app1_fileio extends Applet
    implements ActionListener{

    public JLabel text,clicked;
    public JButton button,clickButton;
    public JTextField textField;

    public void init(){

    setLayout(new BorderLayout(1,1));
    setBackground(Color.white);

    text = new JLabel("Iam simple program");
    clicked = new JLabel("Button Clicked");

    button = new JButton("Click me");
    //add action listener
    button.addActionListener(this);

    clickButton = new JButton("Click again");
    //add action listener
    clickButton.addActionListener(this);

    textField = new JTextField(20);

    //add label and button to panel

    add("North",text);
    add("Center",textField);
    add("South",button);

    } //end of cnst
    public void actionPerformed(ActionEvent eve)
    {
    Object source = eve.getSource();

    JLabel label = new JLabel();

    if(source == button)
    {
    try{
    //get text from textField
    String txt = textField.getText();
    byte b[] = txt.getBytes();
    //create new file object
    String opfilenm = new String("result.txt");

    //create new file object
    File opfile = new File(opfilenm);

    //attach this file object to opstream
    FileOutputStream out = new FileOutputStream(opfile);

    out.write(b);
    out.close();

    //code to read from the file

    String ipfilenm = new String("result.txt");

    //create new file object
    File ipfile = new File(ipfilenm);

    //attach this to ipstream
    FileInputStream ip = new FileInputStream(ipfile);

    byte bb[] = new byte[(int)ipfile.length()];

    int i;
    i = ip.read(bb);

    String dis = new String(bb);

    label.setText(dis);
    ip.close();
    }catch(java.io.IOException e){
    System.out.println("Cannot access result.txt");
    }

    removeAll();
    add("North",label);
    add("Center",label);
    add("South",clickButton);
    validate();
    repaint();
    }
    if(source == clickButton)
    {
    removeAll();
    add("North",text);
    textField.setText("");

    add("Center",textField);
    add("South",button);
    validate();
    repaint();
    }

    }

    public void start(){
    System.out.println("Strating applet.....");
    }

    public void stop(){
    System.out.println("Applet Stopped......");
    }

    public void destroy(){
    System.out.println("Applet destroyed....");
    }


    }//end of class






  2. #2
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Swing Applet

    In your webbrowser , java applets will be running in a restricted environment called "Sandbox"
    To know more about this Sandbox model , check this site ...

    http://java.sun.com/docs/books/tutor...iew/index.html

    Java applets are untrusted codes inside your browser and they cant access the local
    resources. You need to sign your applet to access the local resources. You need to pay some
    $$ to get a certificate.

    Check this post to get the information about signing an applet.

    http://codeguru.developer.com/bbs/wt...age=0&Limit=25

    Poochi..


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured