Results 1 to 8 of 8
  1. #1
    kingkongkill is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default Saving an input.

    Hi I was wondering could someone point out how I get this program to save the file somewhere? Thanks


    Java Code:
    package SimpleWindow;
    
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.SwingUtilities;
    import javax.swing.filechooser.*;
    
    public class RJMC extends JPanel
                                 implements ActionListener {
        static private final String newline = "\n";
        JButton openButton, saveButton;
        JTextArea log;
        JFileChooser fc;
    
        public RJMC() {
            super(new BorderLayout());
    
            log = new JTextArea(10,20);
            log.setMargin(new Insets(50,50,50,50));
            log.setEditable(false);
            JScrollPane logScrollPane = new JScrollPane(log);
    
    
            fc = new JFileChooser();
    
            openButton = new JButton("Open a File...",
                                     createImageIcon("http://www.java-forums.org/images/Open16.gif"));
            openButton.addActionListener(this);
    
            saveButton = new JButton("Save a File...",
                                     createImageIcon("http://www.java-forums.org/images/Save16.gif"));
            saveButton.addActionListener(this);
    
    
            JPanel buttonPanel = new JPanel();
            buttonPanel.add(openButton);
            buttonPanel.add(saveButton);
    
    
            add(buttonPanel, BorderLayout.PAGE_START);
            add(logScrollPane, BorderLayout.CENTER);
        }
    
        public void actionPerformed(ActionEvent e) {
    
    
            if (e.getSource() == openButton) {
                int returnVal = fc.showOpenDialog(RJMC.this);
    
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
    
                    log.append("Opening: " + file.getName() + "." + newline);
                } else {
                    log.append("Open command cancelled by user." + newline);
                }
                log.setCaretPosition(log.getDocument().getLength());
    
    
            } else if (e.getSource() == saveButton) {
                int returnVal = fc.showSaveDialog(RJMC.this);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
    
                    log.append("Saving: " + file.getName() + "." + newline);
                } else {
                    log.append("Save command cancelled by user." + newline);
                }
                log.setCaretPosition(log.getDocument().getLength());
            }
        }
    
        /** Returns an ImageIcon, or null if the path was invalid. */
        protected static ImageIcon createImageIcon(String path) {
            java.net.URL imgURL = RJMC.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("RJMC Code Smells");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
            frame.add(new RJMC());
    
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    UIManager.put("swing.boldMetal", Boolean.FALSE);
                    createAndShowGUI();
                }
            });
        }
    }
    Last edited by kingkongkill; 04-18-2011 at 01:59 PM. Reason: Changing the text to code

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    kingkongkill is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Anything a bit more specific. Haha thats a lot of reading.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Still cross posting.
    Saving an input. (Swing / AWT / SWT / JFace forum at JavaRanch)

    Even after being told about it in
    GUI and Browse (Swing / AWT / SWT / JFace forum at JavaRanch)
    which was never responded to.

    Way to go to get help on a forum!

    db

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Quote Originally Posted by kingkongkill View Post
    thats a lot of reading.
    Give up now. Instead of pretending to try to learn Java, practice saying "Would you like fries with that?"

    db

  6. #6
    kingkongkill is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    cross posting?

  7. #7
    kingkongkill is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    What are you talking about? Asking on 2 separate forums the same question? Trying to get a response that works. I'm just looking for help, you can leave your snide comments at the door, Burke. Thanks

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    ignoreList++
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Hi! Saving data??
    By Gahstanz in forum New To Java
    Replies: 1
    Last Post: 06-29-2010, 06:29 AM
  2. Saving the index??
    By vividcooper in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 10:20 PM
  3. Input technique for unknown lines of input
    By ducreative in forum New To Java
    Replies: 16
    Last Post: 09-23-2009, 09:26 AM
  4. Replies: 4
    Last Post: 03-12-2009, 11:25 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 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
  •