Results 1 to 12 of 12

Thread: JFileChooser

  1. #1
    snus is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default [solved]JFileChooser

    Hey
    I am making a little note program but i can't found out how to open a txt file.
    here is my source code, as you can se i want the jmenuithem "open" to open the txt file

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.* ;
    import java.io.*;
    import javax.swing.JFileChooser;
    
    public class menu{
    	
    	public static void main(String[] args){
    		JFrame frame = new JFrame("Program");
    		frame.setVisible(true);
    		frame.setSize(400,200);
    		frame.setResizable(false);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    	
    		/*
    		 * menubar
    		 */
    		
    		JMenuBar menubar = new JMenuBar();
    		frame.setJMenuBar(menubar);
    		
    		JMenu file = new JMenu("File");
    		menubar.add(file);
    		JMenuItem exit = new JMenuItem("Exit");
    		JMenuItem open = new JMenuItem("Open"); 
    		JMenuItem save = new JMenuItem("Save");
    		file.add(open);
    		file.add(save);
    		file.add(exit);
    		
    		JMenu help = new JMenu("Help");
    		menubar.add(help);
    		JMenuItem about = new JMenuItem("About");
    		help.add(about);
    		
    		JTextArea field = new JTextArea("");
    		frame.add(field);
    		
    		
    		
    	}
    	
    	
    }
    Last edited by snus; 12-21-2010 at 09:31 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    JFileChooser is for selecting a file, not for opening it. Read this tutorial:
    Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)

    db

  3. #3
    snus is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    I have tryed to create a new one. but now it says.

    "The method showOpenDialog(Component) in the type JFileChooser is not applicable for the arguments (new ActionListener(){})"

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import javax.swing.JFileChooser;
    
    
    public class scrollbar {
    	private JFrame f; //Main frame
    	private JTextArea ta; // Text area
    	private JScrollPane sbrText; // Scroll pane for text area
    	private JButton btnQuit; // Quit Program
    	private JButton btnopen; // open button
    	private javax.swing.JFileChooser fileChooser;
    	
    	public scrollbar() {
    		f = new JFrame("Swing demo");
    		f.getContentPane().setLayout(new FlowLayout());
    		
    		//scrollbar
    		ta = new JTextArea("", 5, 50);
    		ta.setLineWrap(true);
    		sbrText = new JScrollPane(ta);
    		sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    		
    		//open button
    		btnopen = new JButton("open");
    		btnopen.addActionListener(
    				new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						
    						int returnVal = fileChooser.showOpenDialog(this);
    					    if (returnVal == JFileChooser.APPROVE_OPTION) {
    					        File file = fileChooser.getSelectedFile();
    					        try {
    					          // What to do with the file, e.g. display it in a TextArea
    					          ta.read( new FileReader( file.getAbsolutePath() ), null );
    					        } catch (IOException ex) {
    					          System.out.println("problem accessing file"+file.getAbsolutePath());
    					        }
    					    } else {
    					        System.out.println("File access cancelled by user.");
    					    }
    							
    					}
    				}
    				);
    	
    		
    		//exit button
    		btnQuit = new JButton("Exit");
    		btnQuit.addActionListener(
    				new ActionListener() {
    					public void actionPerformed(ActionEvent e){
    						System.exit(0);
    					}
    				}
    				);
    	}
    	
    	public void launchFrame(){//create layout
    		//add textarea and button to frame
    		f.getContentPane().add(sbrText);
    		f.getContentPane().add(btnopen);
    		f.getContentPane().add(btnQuit);
    		
    		//close when the close button is clicked
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		//display frame
    		f.pack();
    		f.setVisible(true);	
    	}
    	
    	public static void main(String args[]){
    		scrollbar gui = new scrollbar();
    		gui.launchFrame();
    	}
    }
    Last edited by snus; 12-21-2010 at 03:38 PM.

  4. #4
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Java Code:
    	private javax.swing.JFileChooser  fileChooser = new JFileChooser();
    then
    Java Code:
    						int returnVal = fileChooser.showOpenDialog(null);

  5. #5
    snus is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    thanks that work :)
    But now i cant find out to save the file :(

  6. #6
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

  7. #7
    snus is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    cant solve this problem
    FileWriter cannot be resolved to a type :(

    Java Code:
    //file-open 
    		JMenuItem open = new JMenuItem("Open");
    		open.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				
    				int returnVal = fileChooser.showOpenDialog(null);
    			    if (returnVal == JFileChooser.APPROVE_OPTION) {
    			        File file = fileChooser.getSelectedFile();
    			        try {
    			          // What to do with the file, e.g. display it in a TextArea
    			          ta.read( new FileReader( file.getAbsolutePath() ), null );
    			        } catch (IOException ex) {
    			          System.out.println("problem accessing file"+file.getAbsolutePath());
    			        }
    			    } else {
    			        System.out.println("File access cancelled by user.");
    			    }
    				
    				
    			}
    		});
    		
    		//file-save 
    		JMenuItem save = new JMenuItem("Save");
    		save.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				
    				 JFileChooser jfc = new JFileChooser();
    			      int result = jfc.showSaveDialog(null);
    			      if(result == JFileChooser.CANCEL_OPTION) return;
    			      File file = fileChooser.getSelectedFile();
    			      try {
    			         BufferedWriter bw = new BufferedWriter(new FileWriter(file));
    			         bw.write(ta.getText());
    			         bw.close();
    			      }
    			      catch (Exception e1) {
    			         JOptionPane.showMessageDialog(
    			            null,
    			            e1.getMessage(),
    			            "File Error",
    			            JOptionPane.ERROR_MESSAGE
    			         );
    			      }
    				
    				
    			}
    		});

  8. #8
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    What editor are you using? An IDE like Netbeans would be helpful.

    Did you add the import for java.io.FileWriter?

    If so, show all of the code for help.

  9. #9
    snus is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    thanks forgot to import the filewriter :)
    I am using eclispe. I dont like the drag and drop netbeans are using :)

  10. #10
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Eclipse is good. I would think it would flag missing imports for you, but never used it myself.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Abuse reported on user Java Forums - View Profile: Jaione

    4 posts, all of which are a copy/paste repeat of another post higher up in the thread.

    db

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. jFileChooser
    By ArneBassez in forum AWT / Swing
    Replies: 7
    Last Post: 12-01-2010, 04:38 AM
  2. JFileChooser
    By javabarn in forum New To Java
    Replies: 2
    Last Post: 07-12-2010, 06:08 PM
  3. jfilechooser
    By ranadav in forum AWT / Swing
    Replies: 7
    Last Post: 06-03-2010, 02:44 PM
  4. help with JFileChooser
    By morgatron in forum New To Java
    Replies: 1
    Last Post: 01-08-2010, 09:00 AM
  5. how to use JFileChooser
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •