Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_java thread: Re: I have a question about Swing


Message #1 by "Dave Lorde" <david.lorde@f...> on Wed, 14 Feb 2001 13:09:41
You need to look at the java.io package, especially the File, FileWriter, 
and FileOutputStream classes. For example:

try {
 Writer out = new BufferedWriter(new FileWriter("filename.txt));
 out.write(textArea.getText());
}
catch (IOException e) { /* handle exception */ }

A good source of information, documentation and tutorials on all Java 
topics is Sun's site at java.sun.com.

Tutorials: http://java.sun.com/docs/books/tutorial/
SDK docs: http://www.java.sun.com/products/jdk/1.3/docs/
          http://www.javasoft.com
Discussion Board: http://codeguru.earthweb.com/bbs/wt/wwwthreads.pl?
action=list&Board=java

'Java In A Nutshell' (published by O'Reilly) is a good Java reference.

> I have started something basic.With the code below it compiles and 
runs.It
> has a text field, a horizontal and vertical scroll bar and a basic pull
> down menu.On the menu is open, new, save, saveas.. etc...My question is,
> how would I go about writing the code for the save or saveas.. item on 
the
> pull down menu , that would actually save my text to a Floppy disk.:)Also
> where would I start to look for this kind of information and what is it
> called?Any recommended books? Thank You
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
> import javax.swing.event.*;
> 
> public class MyMenu3 extends JFrame {
> 	
> 	JTextArea textArea;
> 	JScrollPane scrollPane;
> 
> 	public MyMenu3 () {
> 	
> 	JMenuBar menuBar;
> 	JMenu menu;
> 	JMenuItem menuItem;
> 
> 	addWindowListener(new WindowAdapter()
>          {  public void windowClosing(WindowEvent e)
>             {  System.exit(0);
>             }
>          } );
> 
> //Add Components
> 	
> 	Container contentPane = getContentPane();
> 	JPanel panel = new JPanel();
> 	textArea = new JTextArea(300, 300);
> 	scrollPane = new JScrollPane(textArea,
> ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
> ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
> 	contentPane.add(panel, "East");
> 	contentPane.add(scrollPane);
>         
> //Create Menu Bar
> 
> 	menuBar = new JMenuBar();
> 	setJMenuBar(menuBar);
> 
> //Build Menu
> 
> 	menu = new JMenu("File");
> 	menuBar.add(menu);
> 
> //Make menu Item
> 
> 	menuItem = new JMenuItem("Save");
> 	menu.add(menuItem);
> 	}
> 	
> 
> public static void main(String[] args) {
> 	MyMenu3 window = new MyMenu3 ();
> 	window.setSize(300, 300);
> 	window.setVisible(true);
> 	}
>      
> }
> 

  Return to Index