Results 1 to 7 of 7
Thread: JFileChooser with Button
- 09-06-2012, 09:37 AM #1
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();
Now I get this message: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. } }
Unhandled exception type FileNotFoundException at the text: new Scanner(file1).
But if I do that I get the Scanner inputFile1 = newScanner(file1); like this:
But then the problem is again that I can't use code from the while loop part in the try catch.Java Code:try { inputFile1 = new Scanner(file1); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }Last edited by Lund01; 09-06-2012 at 09:41 AM.
- 09-06-2012, 10:03 AM #2
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"); }
- 10-04-2012, 01:29 PM #3
Member
- Join Date
- Sep 2012
- Location
- Guntur, India
- Posts
- 27
- Rep Power
- 0
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
- 10-04-2012, 02:20 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 10-04-2012, 03:43 PM #5
- 10-19-2012, 04:58 PM #6
Member
- Join Date
- Sep 2012
- Location
- Guntur, India
- Posts
- 27
- Rep Power
- 0
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
- 10-19-2012, 09:04 PM #7
Re: JFileChooser with Button
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
How can I to add a mouse event to a button that it'll edit itself and other button
By mad72584 in forum New To JavaReplies: 7Last Post: 08-06-2011, 03:39 AM -
Make a button class that uses your button image.
By eLancaster in forum New To JavaReplies: 1Last Post: 04-26-2011, 11:32 AM -
Adding a sqrt button and percentage button to a calculator
By Josie_Taylor in forum New To JavaReplies: 4Last Post: 03-14-2011, 01:16 AM -
help with JFileChooser
By morgatron in forum New To JavaReplies: 1Last Post: 01-08-2010, 09:00 AM -
Need JFileChooser Help
By Wraithier in forum New To JavaReplies: 4Last Post: 06-18-2008, 05:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks