943,771 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2145
  • Java RSS
Nov 20th, 2008
0

FileChooser

Expand Post »
I wondered if anyone could help. I am trying to use a FileChooser in java. I have got the dialog box to appear and can select the file, but the box just displays "opening" and the file name. how to I add to the code to make it actually open the text file?

here is my current code:

package org.work;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;

public class FileChooser extends JFrame
{

private JTextArea log;
private JFileChooser fc = new JFileChooser();

private String newline = System.getProperty("line.separator");

public FileChooser()
{
super("FileChooserDemo");

JButton openButton = new JButton("Open", new ImageIcon("images/open.gif")); //Create the open button and add image
openButton.addActionListener(new OpenListener()); //Adds action listener to the open button

JButton saveButton = new JButton("Save", new ImageIcon("images/save.gif")); //Create the save button and add image
saveButton.addActionListener(new SaveListener()); //Adds action listener to the save button

JPanel buttonPanel = new JPanel(); //Create a panel
buttonPanel.add(openButton); //Add the open button to the panel
buttonPanel.add(saveButton); //Add the save button to the panel

log = new JTextArea(5,20); //Create the text area
log.setMargin(new Insets(5,5,5,5)); //Set the margins
JScrollPane logScrollPane = new JScrollPane(log);

Container contentPane = getContentPane();
contentPane.add(buttonPanel, BorderLayout.NORTH); //Add the buttons to the panel
contentPane.add(logScrollPane, BorderLayout.CENTER); //Add the text area to the panel
}

private class OpenListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int returnVal = fc.showOpenDialog(FileChooser.this);

if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();

log.append("Opening: " + file.getName() + "." + newline); //Display "Opening" and the retrieved file name in the text area
} else //If not;
{
log.append("Open command cancelled by user." + newline); //Display this command in the text area
}
}
}

private class SaveListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int returnVal = fc.showSaveDialog(FileChooser.this);

if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();

log.append("Saving: " + file.getName() + "." + newline); //Display "Saving" and the retrieved file name in the text area
} else //If not;
{
log.append("Save command cancelled by user." + newline); //Display this command in the text area
}
}
}

public static void main(String s[])
{
JFrame frame = new FileChooser();

frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {System.exit(0);} //Terminates program
});

frame.pack();
frame.setVisible(true); //Set frame to visible
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ebiz is offline Offline
16 posts
since Nov 2008
Nov 20th, 2008
0

Re: FileChooser

With this code you can read files:

Java Syntax (Toggle Plain Text)
  1. File fileName;
  2. //OR
  3. String fileName;
  4.  
  5. BufferedReader reader = new BufferedReader(new FileReader(fileName));
  6.  
  7. String line = reader.readLine();
  8. while (line != null) {
  9. //do whatever you want with the line read.
  10. //perhaps append it to the TextArea.
  11.  
  12. line = reader.readLine();
  13. }
Last edited by javaAddict; Nov 20th, 2008 at 9:40 am.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Nov 20th, 2008
0

Re: FileChooser

Is this within the same class? and do you need to import anything else other than these?

import java.io.*;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ebiz is offline Offline
16 posts
since Nov 2008
Nov 20th, 2008
0

Re: FileChooser

Once you get the file:
File file = fc.getSelectedFile(); use the code to read the file's lines.

Java Syntax (Toggle Plain Text)
  1. File file = fc.getSelectedFile();
  2.  
  3. BufferedReader reader = new BufferedReader(new FileReader(file));
  4.  
  5. String line = reader.readLine();
  6. while (line != null) {
  7. //do whatever you want with the line read.
  8. //perhaps append it to the TextArea.
  9.  
  10. line = reader.readLine();
  11. }
  12. reader.close();

Your imports are fine.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Nov 20th, 2008
0

Re: FileChooser

Thank You for your help.

I have tried adding that code, but the reader is not found, as they are all underlined, and say "unhandled exception type IOExpection".

I added in "File fileName;" from your first post, but this didn't make a difference.

The errors are:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
Unhandled exception type FileNotFoundException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException

at org.project.FileChooser$OpenListener.actionPerformed(FileChooser.java:54)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

i hope this makes sense. thank you.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ebiz is offline Offline
16 posts
since Nov 2008
Nov 20th, 2008
0

Re: FileChooser

Do you know how to catch Exceptions? I assumed that you do.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Nov 20th, 2008
0

Re: FileChooser

thanks for your help. its working now
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ebiz is offline Offline
16 posts
since Nov 2008
Nov 20th, 2008
0

Re: FileChooser

Mark the thread as solved if the issue is resolved.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 21st, 2008
0

Re: FileChooser

Click to Expand / Collapse  Quote originally posted by ebiz ...
thanks for your help. its working now
Did you put BufferedReader in a try-catch block? Do you want an example?
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: NetBeans protected code
Next Thread in Java Forum Timeline: recursionFactorial





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC