Results 1 to 7 of 7
  1. #1
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default JFileChooser with Button

    Hi everyone,

    I am making a program that has multiple buttons. And now I would like when you click on button1 that a JFileChosser opens and that the selected file is read bij the program.
    Now I have made a GUI with buttons and stuff, but I don't get my JFileChooser working. I program Java for a while now, but I never realy made a GUI before.
    So I hope somebody can help me with my JFileChooser.

    This is a part of my code that needs to read in a file.
    Java Code:
    // Create JFileChoosers to import files. 
    JFileChooser chooser1 = new JFileChooser();
    Java Code:
    	// Make that the buttons do a certain action when you press one. 
    	public void actionPerformed(ActionEvent e) {
    		// Open a window where you can select a file1. 
    		if (e.getSource() == button1) {
    			chooser1.showOpenDialog(null); 
    			// Get file 1. 
    			java.io.File file1 = chooser1.getSelectedFile(); 
    			//Create Scanner. 
    			Scanner inputFile1 = new Scanner(file1);
    
    			//
    	               while (inputFile1.hasNext()) {
                                    System.out.println(inputFile1.nextLine()); 
    			
    			}
    			// Close file1. 
    			inputFile1.close(); 
    		}
    		// Open a window where you can select a file2. 
    		else if (e.getSource() == button2) {
    			//commend
    		}
    		//
    		else if (e.getSource() == button3) {
    			//commend
    		}
    		else if (e.getSource() == button4) {
    			//commend
    		}
    		// Close/exit the program. 
    		else if (e.getSource() == button5) {
    			System.exit(0); 
    		}
    		else if (e.getSource() == button6) {
    			// Save file. 
    		}
    	}
    Now I get this message:
    Unhandled exception type FileNotFoundException at the text: new Scanner(file1).

    But if I do that I get the Scanner inputFile1 = newScanner(file1); like this:

    Java Code:
    			try {
    				inputFile1 = new Scanner(file1);
    			} catch (FileNotFoundException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    But then the problem is again that I can't use code from the while loop part in the try catch.
    Last edited by Lund01; 09-06-2012 at 09:41 AM.

  2. #2
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: JFileChooser with Button

    I figured out the problem.
    For anyone who has the same problems as me:

    Java Code:
    			try {
    				Scanner inputFile1 = new Scanner(file1); 
    				while (inputFile1.hasNext()) {
    					System.out.println(inputFile1.nextLine()); 
    					
    				}	
    			} catch (Exception r) {
    				System.out.println("Error");
    			}

  3. #3
    gowthamgutha is offline Member
    Join Date
    Sep 2012
    Location
    Guntur, India
    Posts
    27
    Rep Power
    0

    Default Re: JFileChooser with Button

    chooser1.showOpenDialog(this);

    this represents the current object any Container object. Null don't work. I think so.

    If your JFrame object is like JFrame f=new JFrame();

    then, chooser1.showOpenDialog(f);

    [Moderator edit: link removed]
    Last edited by DarrylBurke; 10-04-2012 at 03:42 PM. Reason: Removed spammy link

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: JFileChooser with Button

    Which has nothing to do with the question in hand, which the OP managed to answer on their own.

    I do hope you aren't spamming the forum like this just to advertise your blog, because one of us moderators will be forced to consider banning you.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: JFileChooser with Button

    Quote Originally Posted by Tolls View Post
    I do hope you aren't spamming the forum like this just to advertise your blog, because one of us moderators will be forced to consider banning you.
    I'm considering it already.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    gowthamgutha is offline Member
    Join Date
    Sep 2012
    Location
    Guntur, India
    Posts
    27
    Rep Power
    0

    Default Re: JFileChooser with Button

    There should be some base for the JFileChooser to come i think so.

    You can use jf.showOpenDialog(baseobj);

    where baseobj may be a JFrame

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: JFileChooser with Button

    Quote Originally Posted by gowthamgutha View Post
    There should be some base for the JFileChooser to come i think so.

    You can use jf.showOpenDialog(baseobj);

    where baseobj may be a JFrame
    Please stop posting wild guesses all over the forum. We try to maintain the quality of responses here, and you may find most of your recent posts deleted; also you may be banned for a period.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 7
    Last Post: 08-06-2011, 03:39 AM
  2. Make a button class that uses your button image.
    By eLancaster in forum New To Java
    Replies: 1
    Last Post: 04-26-2011, 11:32 AM
  3. Adding a sqrt button and percentage button to a calculator
    By Josie_Taylor in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 01:16 AM
  4. help with JFileChooser
    By morgatron in forum New To Java
    Replies: 1
    Last Post: 01-08-2010, 09:00 AM
  5. Need JFileChooser Help
    By Wraithier in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 05:40 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
  •