-
[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
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);
}
}
-
JFileChooser is for selecting a file, not for opening it. Read this tutorial:
Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)
db
-
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(){})"
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();
}
}
-
Code:
private javax.swing.JFileChooser fileChooser = new JFileChooser();
then
Code:
int returnVal = fileChooser.showOpenDialog(null);
-
thanks that work :)
But now i cant find out to save the file :(
-
-
cant solve this problem
FileWriter cannot be resolved to a type :(
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
);
}
}
});
-
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.
-
thanks forgot to import the filewriter :)
I am using eclispe. I dont like the drag and drop netbeans are using :)
-
Eclipse is good. I would think it would flag missing imports for you, but never used it myself.
-
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
-