Solved

Open / Close JFileChooser

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strCount chalenge 3 50
mapBully challenge 6 89
Exception after setting jdbc session management 2 38
for i loop in grovy 1 32
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now