Search code examples
javaapplet

I want to open a file just with one click of button in java in a new window


My file is 1.txt and I want to open it using java such that on pressing a button, that file should open up!!
Is there any command through actionlistener to open the file and patch it with the button??
This is my simple applet program in java..

import java.awt.*;
import java.applet.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CalculatorApplet extends Applet implements ActionListener {

    Button save, view;
    Label fname, lname, email, city, phno;
    TextField t, u, v, w, x;
    Label ans;
    Scanner sc = new Scanner(System.in);

    @Override
    public void init() {
        setLayout(null);

        // create label to display enter no
        fname = new Label("Enter First Name : ");
        fname.setBounds(10, 50, 100, 20);

        lname = new Label("Enter Last Name : ");
        lname.setBounds(10, 70, 100, 20);

        email = new Label("Enter Email : ");
        email.setBounds(10, 90, 80, 20);

        city = new Label("Enter City : ");
        city.setBounds(10, 110, 80, 20);

        phno = new Label("Enter Phno : ");
        phno.setBounds(10, 130, 80, 20);

        // create textbox for entering number
        t = new TextField();
        t.setBounds(120, 50, 200, 20);

        u = new TextField();
        u.setBounds(120, 70, 100, 20);

        v = new TextField();
        v.setBounds(120, 90, 200, 20);

        w = new TextField();
        w.setBounds(120, 110, 80, 20);

        x = new TextField();
        x.setBounds(120, 130, 80, 20);

        // create button for finding sqr
        save = new Button("Save");
        save.setBounds(120, 150, 70, 30);

        // add the action listner on this button
        save.addActionListener(this);

        // create button
        view = new Button("View");
        view.setBounds(190, 150, 70, 30);

        // add the action listner on this button
        view.addActionListener(this);

        // add all the components to the frame
        add(fname);
        add(lname);
        add(email);
        add(city);
        add(phno);
        add(t);
        add(u);
        add(v);
        add(w);
        add(x);
        add(save);
        add(view);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        String fnme = t.getText();
        String lnme = u.getText();
        String emal = v.getText();
        String cty = w.getText();
        String phn = x.getText();

        if (e.getSource() == save) {
            try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("1.txt", true)))) {
                out.println("First Name :" + fnme);
                out.println("Last Name :" + lnme);
                out.println("Email Name : " + emal);
                out.println("City : " + cty);
                out.println("Contact : " + phn);
                out.println("----------------------------------------\n");
                out.println("----------------------------------------\n");
                out.close();
                t.setText("");
                u.setText("");
                v.setText("");
                w.setText("");
                x.setText("");
            } catch (Exception ex) {
                Logger.getLogger(CalculatorApplet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else if(e.getSource() == view){
//            what to type in here????
//            needed code here!!!

                    }
    }
}

I just want to open the file, nothing else..


Solution

  • One can use the Desktop class to let the system open, edit, print, browse a file.

    Desktop.getDesktop().open(file);
    

    This means an external application.

    You might consider writing an HTML file, to make all a bit more stylish.


    After commenting of @MadProgrammer:

    For a sandboxed applet, that has extra security restrictions when run over the internet, it might be better to let the applet open a second window and display the text. The text then need not be written to a file.

    If you want the file to be on your server to save the typed data, that would be a wrong thought: the applet is run in the client's browser.