Solved

Open / Close JFileChooser

Posted on 2004-10-20
4
1,945 Views
Last Modified: 2008-01-09
At First, i want to ask the "Open-Button" if the was clicked. Which Object is it? The "jFileChooser1ActionPerformed" is Cancel. But i want to have Open and cancel too.

And the Second Problem is, I have to save the opened File. Do i have a possibilty to use the jFileChooser as a "Save-Dialog"???

I use Netbeans for my tiny programm.

Please write it easily. i'm a swing newbie:-)

Thanks
0
Comment
Question by:mystixs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12364507
Hi mystixs,

Check the "Using Filechoosers" trail http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html

\t
0
 
LVL 2

Accepted Solution

by:
Breadstick earned 80 total points
ID: 12366550
I hope this is helpful:

-----------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class FileChooserDemo extends JFrame implements ActionListener
{
      private JButton openFile;
      private JButton saveFile;

      public FileChooserDemo()
      {
            super("File Chooser");
            this.setSize(250,70);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            openFile = new JButton("Open File");
            openFile.addActionListener(this);

            saveFile = new JButton("Save File");
            saveFile.addActionListener(this);

            this.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
            this.getContentPane().add(openFile);
            this.getContentPane().add(saveFile);
            this.setVisible(true);
      }

      public void openFileClicked()
      {
            JFileChooser chooser = new JFileChooser();
            chooser.showOpenDialog(this);
            File selectedFile = chooser.getSelectedFile();
            if(selectedFile != null && selectedFile.exists())
            {
                  JOptionPane.showMessageDialog(null, "You selected: " + selectedFile.getName() + " to open.", "File Selected", JOptionPane.INFORMATION_MESSAGE);
            }
      }

      public void saveFileClicked()
      {
            JFileChooser chooser = new JFileChooser();
            chooser.showSaveDialog(this);
            File selectedFile = chooser.getSelectedFile();
            if(selectedFile != null)
            {
                  JOptionPane.showMessageDialog(null, "You selected to save to : " + selectedFile.getName(), "File Selected", JOptionPane.INFORMATION_MESSAGE);
            }
      }

      public void actionPerformed(ActionEvent e)
      {
            Object source = e.getSource();
            if(source == openFile)
            {
                  openFileClicked();
            }
            if(source == saveFile)
            {
                  saveFileClicked();
            }
      }

      public static void main(String[] args)
      {
            new FileChooserDemo();
      }
}
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12367084
Breadstick,

What happened to the "Give a man a fish and you feed him for a day; teach a man to fish and he'll eat forever"?
0
 
LVL 2

Expert Comment

by:Breadstick
ID: 12382822
It's been replaced by "Show a man to fish, and he may give you a few expert points" :]
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 65
How do I remove an object from a 3 61
Eclipse with various Java releases 7 87
learn programming 8 94
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

734 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