Solved

Open / Close JFileChooser

Posted on 2004-10-20
4
1,951 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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

615 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