JTextComponent: write(Writer out) : JTextComponent « javax.swing « Java by API






JTextComponent: write(Writer out)

  

import java.awt.BorderLayout;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class MainClass {
  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;
    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);
  }
}

           
         
    
  








Related examples in the same category

1.JTextComponent.KeyBinding
2.JTextComponent: addCaretListener(CaretListener listener)
3.JTextComponent: getActions()
4.JTextComponent: getCaretPosition()
5.JTextComponent: getDocument()
6.JTextComponent: getHighlighter()
7.JTextComponent: getSelectedText()
8.JTextComponent: getText(int offs, int len)
9.JTextComponent: moveCaretPosition(int pos)
10.JTextComponent: print(MessageFormat headerFormat, MessageFormat footerFormat, boolean showPrintDialog, PrintService service, PrintRequestAttributeSet attributes, boolean interactive)
11.JTextComponent: read(Reader in, Object desc)
12.JTextComponent: replaceSelection(String content)
13.JTextComponent: select(int selectionStart, int selectionEnd)
14.JTextComponent: setDocument(Document doc)
15.JTextComponent: setDragEnabled(boolean b)
16.JTextComponent: setFocusAccelerator(char aKey)
17.JTextComponent: setNavigationFilter(NavigationFilter filter)
18.JTextComponent: setSelectedTextColor(Color c)
19.JTextComponent: setSelectionColor(Color c)
20.JTextComponent: setSelectionEnd(int selectionEnd)
21.JTextComponent: setSelectionStart(int selectionStart)