Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1967
  • Last Modified:

Open / Close JFileChooser

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
mystixs
Asked:
mystixs
  • 2
  • 2
1 Solution
 
Tommy BraasCommented:
Hi mystixs,

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

\t
0
 
BreadstickCommented:
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
 
Tommy BraasCommented:
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
 
BreadstickCommented:
It's been replaced by "Show a man to fish, and he may give you a few expert points" :]
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now