Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Open / Close JFileChooser

Posted on 2004-10-20
4
Medium Priority
?
1,956 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 320 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

715 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