?
Solved

FileDialog example

Posted on 2002-03-31
3
Medium Priority
?
1,019 Views
Last Modified: 2010-05-18
Hello Experts,

Can someone please give me a working example of a filedialog example application?  Not only create the dialog ( I can do that) but open up the file for reading and also open up the dialog for saving the file?

thanks,
Whatever
0
Comment
Question by:whatever080697
3 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 6908667
>  I can do that

what's exactly the problem ?

you'd better post your code and we somebody will change it according to your requirements.
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6908689
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;

public class FileChooserDemo extends JFrame {
    static private final String newline = "\n";

    public FileChooserDemo() {
        super("FileChooserDemo");

        //Create the log first, because the action listeners
        //need to refer to it.
        final JTextArea log = new JTextArea(5,20);
        log.setMargin(new Insets(5,5,5,5));
        log.setEditable(false);
        JScrollPane logScrollPane = new JScrollPane(log);

        //Create a file chooser
        final JFileChooser fc = new JFileChooser();

        //Create the open button
        ImageIcon openIcon = new ImageIcon("images/open.gif");
        JButton openButton = new JButton("Open a File...", openIcon);
        openButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int returnVal = fc.showOpenDialog(FileChooserDemo.this);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    //this is where a real application would open the file.
                    log.append("Opening: " + file.getName() + "." + newline);
                } else {
                    log.append("Open command cancelled by user." + newline);
                }
            }
        });

        //Create the save button
        ImageIcon saveIcon = new ImageIcon("images/save.gif");
        JButton saveButton = new JButton("Save a File...", saveIcon);
        saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int returnVal = fc.showSaveDialog(FileChooserDemo.this);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    //this is where a real application would save the file.
                    log.append("Saving: " + file.getName() + "." + newline);
                } else {
                    log.append("Save command cancelled by user." + newline);
                }
            }
        });

        //For layout purposes, put the buttons in a separate panel
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(openButton);
        buttonPanel.add(saveButton);

        //Explicitly set the focus sequence.
        openButton.setNextFocusableComponent(saveButton);
        saveButton.setNextFocusableComponent(openButton);

        //Add the buttons and the log to the frame
        Container contentPane = getContentPane();
        contentPane.add(buttonPanel, BorderLayout.NORTH);
        contentPane.add(logScrollPane, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        JFrame frame = new FileChooserDemo();

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        frame.pack();
        frame.setVisible(true);
    }
}

0
 
LVL 1

Accepted Solution

by:
mErCuR4 earned 1200 total points
ID: 6909555
//reading file..
FileDialog fileDialog = new FileDialog(new JFrame(), "Load File", FileDialog.LOAD);
          fileDialog.show();
         
          String path = fileDialog.getDirectory()+fileDialog.getFile();
          System.out.println("File Path: "+path);
                //now u can read the file..
          File file = new File(path);
                BufferedReader read = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String s = reader.readLine();
while(s != null)
{
  System.out.println(s);
  s = read.readLine();
}


//save file
FileDialog fileDialog = new FileDialog(new JFrame(), "Save File", FileDialog.SAVE);
String path = fileDialog.getDirectory()+fileDialog.getFile();
          System.out.println("File Path: "+path);
          File file = new File(path);
                PrintWriter writer = new PrintWriter(new FileOutputStream(file), true);
writer.println("hi");
writer.close();
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question