Results 1 to 2 of 2
  1. #1
    tom2zip is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default help with buttons and outputting to a file

    whenever i press the button, the file is created, but it will not have any text in it and i dont know how to fix this
    anyone help?

    heres the code:
    (it may be hard to read the cod
    import java.awt.BorderLayout;
    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import java.awt.FlowLayout;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.ScrollPaneConstants;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.io.*;

    public class ListPage extends JFrame
    {

    private JPanel contentPane;

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

    /**
    * Create the frame.
    */
    public ListPage()
    {
    setVisible(true);
    setTitle("List");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 874, 766);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

    JTextArea list = new JTextArea();
    list.setRows(40);
    list.setColumns(77);
    contentPane.add(list);

    JScrollPane scroll= new JScrollPane(list,ScrollPaneConstants.VERTICAL_SCRO LLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCR OLLBAR_AS_NEEDED);
    contentPane.add(scroll);

    JButton add = new JButton("Add");
    add.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent arg0)
    {
    try
    {
    FileWriter fw = new FileWriter("C:\\Users\\Owner\\Documents\\Output.tx t");
    PrintWriter output = new PrintWriter(fw);

    output.print("HI");
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    });
    contentPane.add(add);
    }

    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: help with buttons and outputting to a file

    Make sure you close the Writer. Your data is still in the buffer and needs to be flushed. Closing the Writer will do this for you.

Similar Threads

  1. looping through vectors and outputting to JTextArea
    By danborgir in forum New To Java
    Replies: 3
    Last Post: 04-25-2011, 08:23 PM
  2. outputting different letters in different colours
    By Sarah24 in forum Java Applets
    Replies: 4
    Last Post: 03-02-2011, 02:32 PM
  3. Help With Outputting Data To A File
    By Prodigial Mouse in forum New To Java
    Replies: 14
    Last Post: 01-24-2011, 08:01 PM
  4. Outputting a * triangle
    By blackbeltsas in forum New To Java
    Replies: 1
    Last Post: 10-17-2010, 11:39 AM
  5. Searching a string from a text file using Swing Buttons
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 2
    Last Post: 09-15-2008, 09:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •