Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Become a Java Expert!

Join 244,295 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 910 people online right now. Registration is fast and FREE... Join Now!





JFileChooser with options

Includes a filter and actions based on selection

Submitted By: William_Wilson
Actions:
Rating:
Views: 4,823

Language: Java

Last Modified: May 5, 2006
Instructions: implement this with a filter, to learn it I am using the Text_files filter previously written

Snippet


  1. [code]
  2. //start path of displayed File Chooser
  3. JFileChooser chooser = new JFileChooser(".\\");
  4. //display FILES_ONLY, DIRECTORIES_ONLY or FILES_AND_DIRECTORIES
  5. chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  6. //set the filter to be used
  7. chooser.setFileFilter(new Text_files());
  8. //display File Chooser and wait for action
  9. int returnVal = chooser.showOpenDialog(view);
  10. //OK/Open button pressed
  11. if (returnVal == JFileChooser.APPROVE_OPTION){
  12.      File directory = chooser.getSelectedFile();
  13.      try{
  14.           //open and display the file with it's information stored in directory
  15.      }
  16.     catch(Exception except) {
  17.         //msg if opening fails
  18.         JOptionPane.showMessageDialog(view, "The File cannot be opened!",
  19.           "Information", JOptionPane.INFORMATION_MESSAGE);
  20.      }
  21. }
  22. [/code]

Copy & Paste


Comments


DR490N5 2009-04-27 11:40:25

What import is needed for JFIleChooser? Would you use javax.swing.JComponent?

William_Wilson 2009-04-27 11:44:45

javax.swing.JFileChooser. documentation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFileChooser.html

adiputra 2009-06-09 21:55:40

William_Wilson : thanks a lot, i'm very newbies with java. do you know? how to start java?


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month