Writing text to a file.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Writing text to a file.

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Location
    Texas.
    Posts
    8

    Writing text to a file.

    I've tried:
    Code:
                    connPort = port.getText();
                    serverIP = server.getText();
                    configF.setVisible(false);
                    try
                    {
                        BufferedWriter w = new BufferedWriter(new FileWriter(new File("sys/conf.txt")));
                        w.write(server.getText().concat("\n"));
                        w.write(port.getText());
                        w.write("This works!");
                        configL.setText("Save successful.");
                    }
                    catch(Throwable t)
                    {
                        configL.setText("Save unsucessful.");
                    }
    Where server and port are JTextAreas holding the server and port, and configL is the label for the window. The label's set to "Save Successful.", but the file is blank after this runs. Can someone please tell me why?
    EDIT 2: Above problem has been fixed. w.close(); Heh.

    EDIT: configF is the JFrame holding all of this, and it should dissappear when I call configF.setVisible(false); but it doesn't. How should I go about making it dissappear?
    Last edited by freeone3000; 10-20-2005 at 06:55 PM. Reason: Fixed first problem.

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Could you post more code? setVisible(false) should work! Try adding a call to dispose().

  3. #3
    Join Date
    Sep 2005
    Location
    Texas.
    Posts
    8
    Code:
    public JPanel createConfigPane()
        {
            final JPanel configPane = new JPanel(new GridBagLayout());
            final JTextField server = new JTextField(serverIP);
            final JTextField port = new JTextField(connPort);
            
            server.setPreferredSize(nameSize);
            port.setPreferredSize(nameSize);
            
            JButton save = new JButton("Save");
            JButton cancel = new JButton("Cancel");
            save.setPreferredSize(new Dimension(70, 30));
            cancel.setPreferredSize(new Dimension(90, 30));
            
            JLabel portL = new JLabel("Port:");
            JLabel serverL = new JLabel("Server address:");
            final JLabel configL = new JLabel("Hiya!");
            final GameGUI g = new GameGUI(new JFrame());
            
            save.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    connPort = port.getText();
                    serverIP = server.getText();
                    configF.setVisible(false);
                    try
                    {
                        BufferedWriter w = new BufferedWriter(new FileWriter(new File("sys/conf.dat")));
                        w.write(server.getText().concat("\n"));
                        w.write(port.getText());
                        configL.setText("Save successful.");
                        w.close();
                    }
                    catch(Throwable t)
                    {
                        configL.setText("Save unsucessful.");
                    }
                }
            });
            cancel.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    configF.setVisible(false);
                    configL.setText("...Ehm, bye?");
                    configF.dispose();
                }
            });
            //GridBagLayout junk.
            
            return configPane;
        }
    I hit "Cancel", and I see "..Ehm, bye?" up on top, where the JLabel is, after I click the "Cancel" button.

  4. #4
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    What is your question now?

    Yes the button's actionlistener puts that text into the configL component.

  5. #5
    Join Date
    Sep 2005
    Location
    Texas.
    Posts
    8
    Well, yes. But that's AFTER I dispose of the frame, and AFTER I set it to be invisible.
    It should be set, but I shouldn't be able to see it.

    Question would be: "How do I make JFrames go away?"
    Last edited by freeone3000; 10-22-2005 at 05:57 PM. Reason: added question

  6. #6
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Cant' explain it. If what you've shown is being execture, setVisible(false) should make close the frame.
    I can only guess that the code isn't executing as you think it is.
    Add some println()s to see where it is executing and when.

  7. #7
    Join Date
    Sep 2005
    Location
    Texas.
    Posts
    8
    Code:
                    configF.setVisible(false);
                    System.out.println("It should be invisible.");
                    configL.setText("...Ehm, bye?");
                    configF.dispose();
                    System.out.println("It should be gone.");
    It is printing out those two bolded lines, but the frame's still visible.
    Last edited by freeone3000; 10-23-2005 at 12:47 PM. Reason: Clarifing

  8. #8
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Only looking at a small part of the program, its hard to say.

    Does configF refer to the ONLY frame? Are there any others?

  9. #9
    Join Date
    Sep 2005
    Location
    Texas.
    Posts
    8
    There are a few other frames, else I would just call System.exit(0);. Generic "frame", which contains the main application frame. It's the only one "there" right now. The other frames are loginF, creditF, helpF, and charF, which are the login screen, credit screen, help screen, and characater creation screen, all uninitialized at this point.

    createConfigPane() is called from a generic ActionListener set to a JButton, config, in the main "frame".

Similar Threads

  1. Transferring Text File
    By Mark in forum Database
    Replies: 0
    Last Post: 07-03-2001, 11:00 AM
  2. Formatting text and writing to it to a file
    By Matt Lake in forum VB Classic
    Replies: 0
    Last Post: 03-13-2001, 08:10 PM
  3. open text file using shell
    By Hian Chew in forum VB Classic
    Replies: 0
    Last Post: 03-06-2001, 12:28 PM
  4. Problem: Reading text file with comma
    By Paolo in forum VB Classic
    Replies: 3
    Last Post: 01-08-2001, 08:59 AM
  5. Replies: 0
    Last Post: 04-17-2000, 01:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center