Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 131,964 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,046 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: 3,571

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


There are currently no comments for this snippet. Be the first to comment!

Add comment


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





Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month