/**I commented everything out dealing with the word count so it works. The problem is that I am not able to read what the file name is. It does not catch the file name, but it prints out the contents of it when it goes to the catch **/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
public class FileDialogDemo extends JFrame implements ActionListener
{
private JMenuItem jmiOpen, jmiSave, jmiExit, jmiAbout, jmiSpell, jmiReplace, jmiWCount, jmiHelp_me;
private JTextArea jtaFile= new JTextArea();
private JTextField jtfFilename=new JTextField(12);
private JLabel jlblStatus =new JLabel();
private JFileChooser jFileChooser =new JFileChooser();
public static void main(String []args)
{
FileDialogDemo frame=new FileDialogDemo();
frame.setSize(500, 500);
frame.setVisible(true);
}
public FileDialogDemo()
{
setTitle("Test JFileChooser ");
JMenuBar mb=new JMenuBar();
setJMenuBar(mb);
JMenu fileMenu = new JMenu("File");
mb.add(fileMenu);
JMenu EditMenu = new JMenu("Edit");
mb.add(EditMenu);
JMenu HelpMenu = new JMenu("Help");
mb.add(HelpMenu);
fileMenu.add(jmiOpen = new JMenuItem("Open"));
fileMenu.add(jmiSave = new JMenuItem("Save"));
fileMenu.add(jmiExit = new JMenuItem("Exit"));
fileMenu.add(jmiAbout = new JMenuItem("About"));
EditMenu.add(jmiReplace = new JMenuItem("Replace"));
EditMenu.add(jmiSpell = new JMenuItem("Spell"));
EditMenu.add(jmiWCount = new JMenuItem("Word Count"));
HelpMenu.add(jmiHelp_me=new JMenuItem("Help"));
//jHelpChooser.setCurrentDirectory(new Help("."));
getContentPane().add(new JScrollPane(jtaFile),
BorderLayout.CENTER);
getContentPane().add(jlblStatus, BorderLayout.SOUTH);
jmiOpen.addActionListener(this);
jmiSave.addActionListener(this);
jmiAbout.addActionListener(this);
jmiExit.addActionListener(this);
jmiHelp_me.addActionListener(this);
jmiWCount.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String actionCommand =e.getActionCommand();
if(e.getSource() instanceof JMenuItem)
{
if("Open".equals(actionCommand))
open();
else if("Save".equals(actionCommand))
save();
else if("About".equals(actionCommand))
{
JOptionPane.showMessageDialog(this, "Demonstrate Using File Dialogs", "About this Demo", JOptionPane.INFORMATION_MESSAGE);
}
else if("Exit".equals(actionCommand))
System.exit(0);
else if("Help".equals(actionCommand))
JOptionPane.showMessageDialog(null, "YOU ARE STUPID IF YOU NEED TO USE HELP");
//else if ("Word Count".equals(actionCommand))
// count();
}
}
private void open()
{
if(jFileChooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
{
open(jFileChooser.getSelectedFile());
}
}
private void open(File file)
{
try{
BufferedInputStream in =new BufferedInputStream(new FileInputStream(file));
byte[] b = new byte[in.available()];
in.read(b, 0, b.length);
jtaFile.append(new String(b, 0, b.length));
in.close();
jlblStatus.setText(file.getName() + " Opened");
}
catch (IOException ex)
{
jlblStatus.setText("Error opening " + file.getName());
}
}
private void save()
{
if(jFileChooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
{
save(jFileChooser.getSelectedFile());
}
}
private void save(File file)
{
try{
BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream(file));
byte[] b = (jtfFilename.getText()).getBytes();
out.write(b, 0, b.length);
out.close();
jlblStatus.setText(file.getName() + " Saved ");
}
catch(IOException ex)
{
jlblStatus.setText("Error saving " + file.getName());
}
}
/** private static void count(String name, BufferedReader in) throws
IOException {
long numLines = 0;
long numWords = 0;
long numChars = 0;
String line;
do {
line = in.readLine();
if (line != null)
{
numLines++;
numChars += line.length();
numWords += countWords(line);
}
}
while (line != null);
System.out.println(name + "\t" + numLines + "\t" +
numWords + "\t" + numChars);
}
private static void count(String fileName) {
BufferedReader in = null;
try {
FileReader fileReader = new FileReader(fileName);
in = new BufferedReader(fileReader);
count(fileName, in);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}*/
}
michelle13 0 Newbie Poster
cosi 4 Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.