Example usage for javax.swing JTextField write

List of usage examples for javax.swing JTextField write

Introduction

In this page you can find the example usage for javax.swing JTextField write.

Prototype

public void write(Writer out) throws IOException 

Source Link

Document

Stores the contents of the model into the given stream.

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField nameTextField = new JTextField();
    frame.add(nameTextField, BorderLayout.NORTH);

    FileWriter writer = null;// ww  w  .j a  va 2 s. c om
    try {
        writer = new FileWriter("filename.txt");
        nameTextField.write(writer);
    } catch (IOException exception) {
        System.err.println("Save oops");
        exception.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException exception) {
                System.err.println("Error closing writer");
                exception.printStackTrace();
            }
        }
    }

    frame.setSize(250, 100);
    frame.setVisible(true);
}

From source file:LabelSampleSaveText.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Label Focus Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Name: ");
    label.setDisplayedMnemonic(KeyEvent.VK_N);
    JTextField textField = new JTextField();
    label.setLabelFor(textField);//from   w  ww. java 2  s  .  c o  m
    panel.add(label, BorderLayout.WEST);
    panel.add(textField, BorderLayout.CENTER);
    frame.add(panel, BorderLayout.NORTH);
    frame.add(new JButton("Somewhere Else"), BorderLayout.SOUTH);
    frame.setSize(250, 150);
    frame.setVisible(true);

    textField.setText("your text");
    String filename = "test.txt";

    FileWriter writer = null;
    try {
        writer = new FileWriter(filename);
        textField.write(writer);
    } catch (IOException exception) {
        System.err.println("Save oops");
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException exception) {
                System.err.println("Error closing writer");
                exception.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Name: ");
    label.setDisplayedMnemonic(KeyEvent.VK_N);
    JTextField textField = new JTextField();
    label.setLabelFor(textField);/*from  w  w w.j  a v  a  2 s . c o m*/
    panel.add(label, BorderLayout.WEST);
    panel.add(textField, BorderLayout.CENTER);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel, BorderLayout.NORTH);
    frame.add(new JButton("Somewhere Else"), BorderLayout.SOUTH);
    frame.setSize(250, 150);
    frame.setVisible(true);

    textField.setText("your text");
    String filename = "test.txt";

    FileWriter writer = new FileWriter(filename);
    textField.write(writer);
    writer.close();

}