| | |
FileChooser
Thread Solved
![]() |
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 0
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
}
}
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
}
}
With this code you can read files:
Java Syntax (Toggle Plain Text)
File fileName; //OR String fileName; BufferedReader reader = new BufferedReader(new FileReader(fileName)); String line = reader.readLine(); while (line != null) { //do whatever you want with the line read. //perhaps append it to the TextArea. line = reader.readLine(); }
Last edited by javaAddict; Nov 20th, 2008 at 9:40 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Once you get the file:
Your imports are fine.
File file = fc.getSelectedFile(); use the code to read the file's lines. Java Syntax (Toggle Plain Text)
File file = fc.getSelectedFile(); BufferedReader reader = new BufferedReader(new FileReader(file)); String line = reader.readLine(); while (line != null) { //do whatever you want with the line read. //perhaps append it to the TextArea. line = reader.readLine(); } reader.close();
Your imports are fine.
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 0
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.
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.
Do you know how to catch Exceptions? I assumed that you do.
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- Comparing Images For Similarity (Java)
- JApplet display problem (Java)
- Source Code that don't work? (Java)
- Hey check out my prog. It has an error can u find it? (Java)
- Help with a reservation program! GUI Messed XD (Java)
- "cannot resolve symbol"problem (Java)
- Java.io help!!! (Java)
- PLS Help newbie (Java)
Other Threads in the Java Forum
- Previous Thread: NetBeans protected code
- Next Thread: recursionFactorial
| Thread Tools | Search this Thread |
911 actionlistener addball addressbook android api applet application array automation binary block bluetooth button character class client code component consumer css csv database desktop developmenthelp eclipse ee error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia jvm lego linked linux loan mac map method mobile netbeans notdisplaying number objects online oriented phone printf problem program programming project projects recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set singleton sms software sort sql swing system test textfields threads time title tree tutorial-sample ubuntu update windows working






