0

Ok I am trying to get this applet to write to a file, what am I missing? It creates the file but won't write to the file, and just proceeds to clear the boxes to prepare for the next entry. I have done it this way before but not with an Applet. Can anyone help me please. Thanks

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import static java.nio.file.StandardOpenOption.CREATE;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class collectGui extends JApplet implements ActionListener {

    // PAINT METHOD
    public void paint(Graphics g)
    {
        Font font = new Font("Arial", Font.BOLD, 18);
        g.setFont(font);
        g.setColor(Color.WHITE);
        g.drawString("Enter Client Name And Phone Number", 40, 25);
        resize(400, 400);
        this.setBackground(Color.BLUE);
    }

    // CREATES OBJECTS
    private static final long serialVersionUID = 1L;
    TextField text1;
    TextField text2;
    Label label1, label2,label3;
    Button button;
    Font font = new Font("Arial", Font.BOLD, 11);

    // INIT METHOD
    public void init() {
        setLayout(null);
        repaint();

        // YOUR CLIENTS NAME
        label1 = new Label("Clients Name: ");
        label1.setBounds(65, 47, 150, 25);
        setFont(font);
        add(label1);

        // CLIENT NAME ENTRY
        text1 = new TextField(5);
        text1.setBounds(225, 46, 125, 25);
        add(text1);

        // CLIENT PHONE NUMBER
        label2 = new Label("Client Phone Number: ");
        label2.setBounds(65, 100, 150, 25);
        setFont(font);
        add(label2);

        // CLIENT PHONE NUMBER ENTRY BOX
        text2 = new TextField(5);
        text2.setBounds(225, 100, 125, 25);
        add(text2);

        //LABEL 3 TELLS FILE WAS WRITTEN
        label3 = new Label ("");
        label3.setBounds(50,235,300,125);
        setFont(font);
        add(label3);

        // BUTTON
        button = new Button("Enter");
        button.setBounds(160, 175, 100, 20);
        add(button);

        // ACTION LISTENER
        button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
       //CODE TO WRITE TO FILE
           String number = text2.getText();
           //Integer.parseInt(number);
            String name = text1.getText();
            try
            {
            Path file =
            Paths.get("C:\\Users\\User\\Desktop\\college     assignments\\1Java2\\FinalApp\\Data.txt");
            label3.setText("             You Created A  Data.txt file");
                String s = "";
                String delimiter = ",";
                OutputStream output = new BufferedOutputStream(Files.newOutputStream(file,CREATE));
                 BufferedWriter writer = new
                 BufferedWriter(new OutputStreamWriter(output));

                //FORMATS THE WAY THE ENTRY IS SAVED TO THE FILE
                    s = "Name =  " + name + delimiter + "Number =  " + number;
                    writer.write(s, 0, s.length());
                    writer.newLine();
                    //CLEAR
                    text1.setText("");
                    text2.setText("");
                    writer.close();
            }
            catch(Exception e) {
                    label3.setText("message:  " + e);
            } 
        }
    //output.flush();
    //writer.close();
    //input.close();
    //x.close();

//output.flush();
//writer.close();

//input.close();
//x.close();
    }   
Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
user2796618
  • 43
  • 2
  • 5
  • 1
    `catch(Exception e) { label3.setText("message: " + e); }` Change code of the form `catch (Exception e) { ..` to `catch (Exception e) { e.printStackTrace(); // very informative! ..`. Ensure the [Java Console](http://www.java.com/en/download/help/javaconsole.xml) is configured to show for applets & JWS apps. If there is no output at the default level, raise it and try again. It is probably 'permissions'. – Andrew Thompson Sep 26 '13 at 16:12
  • 2
    Don't mix Swing & AWT. Also Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Sep 26 '13 at 16:13
  • 1
    1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) A single blank line of white space in source code is *always* enough. Blank lines after `{` or before `}` are also typically redundant. 3) `resize(400, 400);` Don't call that in an applet, ever. Further, never call it in a paint method (applet or otherwise)! – Andrew Thompson Sep 26 '13 at 16:13

0 Answers0