-1
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import java.awt.CardLayout;
import javax.swing.JSplitPane;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.awt.event.ActionEvent;
import javax.swing.JEditorPane;
import java.awt.Color;
import java.awt.Font;
import javax.swing.DropMode;
import javax.swing.JScrollBar;
import javax.swing.SwingConstants;

public class Program extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;

    /**
     * Launch the application.
     */

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Program frame = new Program();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    /**
     * Create the frame.
     *
     * @throws BadLocationException
     */
    public Program() throws BadLocationException {
        double points = 0;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(0, 0, 1200, 760);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JEditorPane dtrpnTypeTextHere = new JEditorPane();
        dtrpnTypeTextHere.setContentType("type/normal");
        dtrpnTypeTextHere.setToolTipText("");
        dtrpnTypeTextHere.setFont(new Font("Arial", Font.PLAIN, 16));
        dtrpnTypeTextHere.setForeground(Color.GREEN);
        dtrpnTypeTextHere.setBackground(Color.BLACK);
        dtrpnTypeTextHere.setBounds(10, 23, 1152, 671);
        contentPane.add(dtrpnTypeTextHere);
        int length = dtrpnTypeTextHere.getDocument().getLength();
        String text = dtrpnTypeTextHere.getDocument().getText(0, length);
        JLabel lblNewLabel = new JLabel("Power Text Editor");
        lblNewLabel.setBackground(Color.BLACK);
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setForeground(Color.GREEN);
        lblNewLabel.setBounds(515, 0, 125, 14);
        contentPane.add(lblNewLabel);
        JLabel lblFileName = new JLabel("File Name:");
        lblFileName.setBounds(660, 703, 75, 14);
        contentPane.add(lblFileName);
        JButton btnFinishEditing = new JButton("Finish editing");
        btnFinishEditing.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                BufferedWriter writer = null;
                try {
                    //create a temporary file
                    File logFile = new File("yas.txt");

                    // This will output the full path where the file will be written to...
                    System.out.println(logFile.getCanonicalPath());
                    writer = new BufferedWriter(new FileWriter(logFile));
                    writer.write(text);
                } catch (Exception e1) {
                    e1.printStackTrace();
                } finally {
                    try {
                        // Close the writer regardless of what happens...
                        writer.close();
                    } catch (Exception e1) {
                    }

                    System.out.println("Quit because of user exit");
                    System.exit(0);

                }
            }
        });
        btnFinishEditing.setForeground(Color.GREEN);
        btnFinishEditing.setBackground(Color.BLACK);
        btnFinishEditing.setBounds(532, 699, 108, 23);
        contentPane.add(btnFinishEditing);

        textField_1 = new JTextField();
        textField_1.setBounds(726, 700, 131, 20);
        contentPane.add(textField_1);
        textField_1.setColumns(10);

    }
}

There is the code, I'm having issues when making the file, I when I type things in, it appears inside the file as blank.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • `contentPane.setLayout(null);` 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). – Andrew Thompson Aug 11 '15 at 00:06
  • Look at [`flush()`](http://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html#flush--) as well as [`JTextComponent.write(Writer)`](http://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html#write-java.io.Writer-) .. – Andrew Thompson Aug 11 '15 at 00:09

2 Answers2

2

You are operating in an Event Driven Environment, that means between creating the components and showing the UI and something happening, time has passed.

This means that when you do something like...

        JEditorPane dtrpnTypeTextHere = new JEditorPane();
        dtrpnTypeTextHere.setContentType("type/normal");
        dtrpnTypeTextHere.setToolTipText("");
        dtrpnTypeTextHere.setFont(new Font("Arial", Font.PLAIN, 16));
        dtrpnTypeTextHere.setForeground(Color.GREEN);
        dtrpnTypeTextHere.setBackground(Color.BLACK);
        dtrpnTypeTextHere.setBounds(10, 23, 1152, 671);
        contentPane.add(dtrpnTypeTextHere);
        int length = dtrpnTypeTextHere.getDocument().getLength();
        String text = dtrpnTypeTextHere.getDocument().getText(0, length);

And then something like...

btnFinishEditing.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        BufferedWriter writer = null;
        try {
            //create a temporary file
            File logFile = new File("yas.txt");

            // This will output the full path where the file will be written to...
            System.out.println(logFile.getCanonicalPath());
            writer = new BufferedWriter(new FileWriter(logFile));
            writer.write(text);
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {
                // Close the writer regardless of what happens...
                writer.close();
            } catch (Exception e1) {
            }

            System.out.println("Quit because of user exit");
            System.exit(0);

        }
    }
});

happens, the value of text won't have changed, but the contents of the JEditorPane will have. There is simply no relationship between the value you assign to text and the contents of the JEditorPane except the moment in time you do it.

So, instead of doing this...

int length = dtrpnTypeTextHere.getDocument().getLength();
String text = dtrpnTypeTextHere.getDocument().getText(0, length);

In the constructor, you should do it in the ActionListener...

btnFinishEditing.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        int length = dtrpnTypeTextHere.getDocument().getLength();
        String text = dtrpnTypeTextHere.getDocument().getText(0, length);
        BufferedWriter writer = null;
        try {
            //create a temporary file
            File logFile = new File("yas.txt");

            // This will output the full path where the file will be written to...
            System.out.println(logFile.getCanonicalPath());
            writer = new BufferedWriter(new FileWriter(logFile));
            writer.write(text);
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {
                // Close the writer regardless of what happens...
                writer.close();
            } catch (Exception e1) {
            }

            System.out.println("Quit because of user exit");
            System.exit(0);

        }
    }
});

as an example.

You might also like to have a look at The try-with-resources Statement, which will reduce the complexity of your file writing try-catch-finally block ;)

As a side note, avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Your method Program is run once at the beginning of the program to set up the UI. You are declaring and setting the variable text at the time the Program is run. Given that no text has been typed when you are initializing the program, you will always get an empty string in your file.

What you need to do is to move the variable text and the code that reads the text from the JEditorPane dtrpnTypeTextHere to the beginning of the action.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433