-3

I am making a program for my friend. I ran into an error while trying to create a write and save text into a .txt file. I should note this is the first time I have ever created something in Java Swing.

The error is marked with an asterisk.

    package com.laganstoop.me;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JCheckBox;

// Created by: Laganstoop (David L. Perez)
// Do Not Distribute!

public class Main implements ActionListener {

    private JFrame frame;

     public static String infoBox(String infoMessage, String titleBar)
        {
            JOptionPane.showMessageDialog(null, infoMessage, "" + titleBar, JOptionPane.INFORMATION_MESSAGE);
            return "";
        }
     public static void errorMessage(String errorMessage, String titleBar)
     {
         JOptionPane.showMessageDialog(null, errorMessage, "" + titleBar, JOptionPane.INFORMATION_MESSAGE);
     }



    public static final int HEIGHT = 800;
    public static final int WIDTH = 600;
    public static final int nHEIGHT = 400;
    public static final int nWIDTH = 300;

    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main window = new Main();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Main() {
        initialize();
    }



    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {

        frame = new JFrame();
        frame.getContentPane().setBackground(Color.LIGHT_GRAY);
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(HEIGHT, WIDTH);

        JButton btnNewButton = new JButton("Click To Add New");
        btnNewButton.setBounds(10, 11, 132, 23);
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e1) {

            e1.printStackTrace();
        } catch (InstantiationException e1) {

            e1.printStackTrace();
        } catch (IllegalAccessException e1) {

            e1.printStackTrace();
        } catch (UnsupportedLookAndFeelException e1) {

            e1.printStackTrace();
        }
        frame.getContentPane().setLayout(null);
        frame.getContentPane().add(btnNewButton);

        JPanel panel = new JPanel();
        panel.setBounds(10, 11, 764, 181);
        panel.setBackground(Color.WHITE);
        panel.setForeground(Color.WHITE);
        frame.getContentPane().add(panel);
        panel.setLayout(null);
        panel.hide();

        JLabel lblName = new JLabel("Name:");
        lblName.setFont(new Font("Yu Gothic", Font.BOLD, 13));
        lblName.setBounds(10, 11, 46, 14);
        panel.add(lblName);

        JLabel lblDate = new JLabel("Date:");
        lblDate.setFont(new Font("Yu Gothic", Font.BOLD, 13));
        lblDate.setBounds(10, 36, 46, 14);
        panel.add(lblDate);

        JLabel lblLocation = new JLabel("Time:\r\n");
        lblLocation.setFont(new Font("Yu Gothic", Font.BOLD, 13));
        lblLocation.setBounds(10, 61, 60, 14);
        panel.add(lblLocation);

        JLabel lblLocation_1 = new JLabel("Location:");
        lblLocation_1.setFont(new Font("Yu Gothic", Font.BOLD, 13));
        lblLocation_1.setBounds(10, 86, 60, 14);
        panel.add(lblLocation_1);

        JLabel lblRe = new JLabel("Referee");
        lblRe.setFont(new Font("Yu Gothic", Font.BOLD, 13));
        lblRe.setBounds(10, 111, 60, 14);
        panel.add(lblRe);

        textField = new JTextField();
        textField.setBounds(76, 8, 127, 20);
        panel.add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(76, 33, 127, 20);
        panel.add(textField_1);

        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(76, 58, 127, 20);
        panel.add(textField_2);

        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(76, 86, 127, 20);
        panel.add(textField_3);

        textField_4 = new JTextField();
        textField_4.setColumns(10);
        textField_4.setBounds(76, 108, 127, 20);
        panel.add(textField_4);

        JCheckBox checkBox = new JCheckBox("");
        checkBox.setBounds(209, 7, 21, 23);
        panel.add(checkBox);

        JCheckBox checkBox_1 = new JCheckBox("");
        checkBox_1.setBounds(209, 32, 21, 23);
        panel.add(checkBox_1);

        JCheckBox checkBox_2 = new JCheckBox("");
        checkBox_2.setBounds(209, 57, 21, 23);
        panel.add(checkBox_2);

        JCheckBox checkBox_3 = new JCheckBox("");
        checkBox_3.setBounds(209, 82, 21, 23);
        panel.add(checkBox_3);

        JCheckBox checkBox_4 = new JCheckBox("");
        checkBox_4.setBounds(209, 107, 21, 23);
        panel.add(checkBox_4);

        JPanel panel_2 = new JPanel();
        panel_2.setBounds(20, 290, 358, 210);
        panel_2.setLayout(null);

        JButton btnNewNote = new JButton("New Note");
        btnNewNote.setFont(new Font("Yu Gothic", Font.BOLD, 16));
        btnNewNote.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFrame noteFrame = new JFrame();
                noteFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                noteFrame.setLocationRelativeTo(panel);
                noteFrame.pack();
                noteFrame.setSize(nHEIGHT, nWIDTH);
                noteFrame.getContentPane().add(panel_2);

                noteFrame.setVisible(true);

            }
        });
        btnNewNote.setBounds(293, 16, 461, 151);
        panel.add(btnNewNote);
        JLabel lblNotes = new JLabel("Notes:");
        lblNotes.setBounds(10, 0, 46, 23);
        panel_2.add(lblNotes);
        lblNotes.setFont(new Font("Yu Gothic", Font.BOLD, 13));

        JButton btnSaveCreate = new JButton("Create & Save");
        btnSaveCreate.setBounds(237, 177, 131, 23);
        panel_2.add(btnSaveCreate);

        JCheckBox checkBox_5 = new JCheckBox("");
        checkBox_5.setBounds(210, 177, 21, 23);
        panel_2.add(checkBox_5);









        btnSaveCreate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String NAME = textField.getText();
                String DATE = textField_1.getText();
                String TIME = textField_2.getText();
                String LOCATION = textField_3.getText();
                String Referee = textField_4.getText();

                if(checkBox.isSelected() && checkBox_1.isSelected() && checkBox_2.isSelected() && checkBox_2.isSelected() && checkBox_3.isSelected() && checkBox_4.isSelected() && checkBox_5.isSelected())
                {
                    infoBox("Met with " + NAME + " on " + DATE + " @" + TIME + ", and went to " + LOCATION + ", then I was introduced by " + Referee, "Paragraph");
                }
                else {
                    errorMessage("Error: Please validate entries!", "Error!");
                }





            }
        });

        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                btnNewButton.hide();
                panel.setVisible(true); 

            }

        });

    try {
        String content = textField_5.getText(); 
                File file = new File("/users/David/filename.txt");

                // if file doesnt exists, then create it
                if (!file.exists()) {
                    file.createNewFile();
                }

                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(content);    //step2: write it
                bw.close();

                System.out.println("Done");

            } catch (IOException e) {
                e.printStackTrace(); **** ***** 


            }
    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
    }
}

Any Help is greatly appreciated!

Laganstoop
  • 11
  • 1
  • 2
    And how do you understand the error message ? Looks pretty clear – Dici Aug 20 '16 at 23:45
  • 4
    Actually read the error and debug. It gives you the problem – Andrew Li Aug 20 '16 at 23:45
  • 2
    *"I am making a program for my friend"* Then your friend should be posting here, not you. – Andrew Thompson Aug 20 '16 at 23:50
  • 1
    The first advice I would offer 'your friend' is.. 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). .. – Andrew Thompson Aug 20 '16 at 23:52
  • 2
    .. 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! IDEs usually have a shortcut for formatting code, and doing so would help make the source of the error more obvious. – Andrew Thompson Aug 20 '16 at 23:54

1 Answers1

1

You just forgot to add the } in this statement:

catch (IOException e) {
  e.printStackTrace();
}

Just as Andrew Thompson said: simple indentation would have made this error a lot easier to spot if done properly, and should be used as common practice.

Cornchip
  • 334
  • 3
  • 12