Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview Project 2 JFileChooser StringTokenizers Vectors/arrays.

Similar presentations


Presentation on theme: "Overview Project 2 JFileChooser StringTokenizers Vectors/arrays."— Presentation transcript:

1 Overview Project 2 JFileChooser StringTokenizers Vectors/arrays

2 JFileChooser javax.swing.JFileChooser used to create file choosers for selecting files or directories to open or save. Platform independent

3 Open, Save, & Customized Dialog Note: r is an int, fc is a JFileChooser r = fc.showOpenDialog(owner); button labeled "Open" r = fc.showSaveDialog(owner); button labeled "Save" r = fc.showDialog(owner, title); Custom button. (i.e. “Attach”) owner parameter is the component (eg, JFrame, JPanel,...) over which the dialog should be centered. title parameter is a string that is used as the dialog's title and accept button text.

4 Common Methods By default a file chooser allows the user to select only files. fc.setFileSelectionMode(… JFileChooser.FILES_ONLY); // DEFAULT JFileChooser.DIRECTORIES_ONLY); JFileChooser.FILES_AND_DIRECTORIES);

5 Create a JFileChooser //JFileChooser using the default directory JFileChooser fc = new JFileChooser(); fc.showOpenDialog(parentComponent) //String representation of the path JFileChooser fc = new JFileChooser("C:\\temp\\"); fc.showOpenDialog(parentComponent) where the 'parentComponent' is normally a JFrame

6 Handling the response /*"this" in the below code is the ParentComp that acts as the parent to the JFileChooser dialog.*/ int returnVal = fc.showOpenDialog(this); //declare the file object File selectedFile = null; //OR handle multiple files with // File[ ] selectedFiles;

7 Handling user response //Query the JFC to get input from the user if(returnVal == JFileChooser.APPROVE_OPTION) { selectedFile = fc.getSelectedFile(); // OR // selectedFiles = fc.getSelectedFiles(); //to handle multiple returns. } else break; Example: 3 (open files only) & 3b (open directories and files)

8 Save a file public static boolean writeFile(File file, String string) { BufferedWriter fouthandle = new BufferedWriter(new FileWriter(file) ); fouthandle.write(string, 0, string.length() ); fouthandle.flush(); fouthandle.newLine(); } Example 3d Line 210 & 253.

9 Save File public static boolean writeFile(File file, String dataString) { try { BufferedWriter fouthandle = new BufferedWriter(new FileWriter(file) ); fouthandle.write(dataString, 0, dataString.length() ); fouthandle.flush(); fouthandle.newLine(); } catch (FileNotFoundException e) { System.out.println(“File Not Found”); }

10 JFileChooser Tips Why should a file chooser be an instance variable? A file chooser remembers the directory that was last used so any reuse opens in the same directory. It is also more efficient since it is created only once and customizations only have to be done once.

11 File Filters User to view only those files of interest Text, html, psd, etc. Why use a filter? Ease of use Error prevention

12 How to implement Write a class that extends the FileFilter class an abstract class, and implements the following methods: boolean accept(java.io.File file) String getDescription() Just one line of code to activate the FileFilter class. Passes file to the FileFilter objects Determine whether it should be displayed to the user. 1.If the file is actually a directory, then should this directory be displayed to the user? 2.Does the file have the extension(s) which the user wants to see? Example of File filter: 3c file filter, 3d save a file

13 It’s Open….now What StringTokenizer: used to break strings into tokens (words, numbers, operators, or whatever). Constructors StringTokenizer(String str) Constructs a string tokenizer for the specified string. StringTokenizerString StringTokenizer(String str, String delim) Constructs a string tokenizer for the specified string. StringTokenizerString StringTokenizer(String str, String delim, bool returnDelims) Constructs a string tokenizer for the specified string. StringTokenizerString

14 String Class Revisited String info = “Catanzaro, Project 1, 100%” int name = info.indexOf(‘ ’); int grade = info.lastIndexOf(‘,’); This was just for ONE Line, ONE String…. How about Multiple lines CSV, Tab delimited, etc.

15 StringTokenizer: Methods Assume that st is a StringTokenizer. st.hasMoreTokens() Returns true if there are more tokens. st.nextToken() Returns the next token as a String. st.countTokens() Returns the int number of tokens.

16 StringTokenizer methods & code // Assume s contains a string of words… String longestWord = ""; StringTokenizer st = new StringTokenizer(s, “,”); while (st.hasMoreTokens()) { String w = st.nextToken(); if (w.length() > longestWord.length()) { longestWord = w; } Example 3e TokenTest

17 Data Collection How should you store this data? What is the data type? How much data, do you know? Probably A Vector (Array on steroids)

18 Common Vector Methods addadd(int index, Object element) Inserts the specified element at the specified position in this Vector.Object addadd(Object o) Appends the specified element to the end of this Vector.Object elementAtelementAt(int index) Returns the component at the specified index. elementselements() Returns an enumeration of the components of this vector. firstElementfirstElement() Returns the first component (the item at index 0) of this vector removeRangeremoveRange(int fromIndex, int toIndex) Removes from this List all of the elements in the range.

19 Pro’s & Cons Arrays Efficiency Vectors size objects

20 Getting the data Enumeration myEnum = v.elements(); while(myEnum.hasMoreElements()){ //do something } --------------------------------------------------------- for (Enumeration e = v.elements() ; e.hasMoreElements() ;){ System.out.println(e.nextElement()); }

21 How to store what you collect How should your store you data? Strings, chars, shapes,… Example 5b ReadAndCollect

22 Projects are not limited to spec Error checking Color chooser File filters Create multiple file formats Polygons Filled shapes Etc.


Download ppt "Overview Project 2 JFileChooser StringTokenizers Vectors/arrays."

Similar presentations


Ads by Google