I'm working on building a simple WAV to BIN conversion application

Viewed 21

Apologies in advance for the long post.

I have currently succeeded in creating a Java class that converts WAV files to BIN files, though I'm currently struggling to build the physical application which allows a user to select a WAV file from their computer and press a button to convert said file into a BIN file. I was informed today by VSCode that JFrame.show() is now an obsolete function. As it has also been roughly seven months since I've used Java, I've encountered some Exceptions in my code, such as

Exception in thread "main" java.lang.ClassCastException: class javax.swing.JFileChooser cannot be cast to class java.awt.event.ActionListener (javax.swing.JFileChooser and java.awt.event.ActionListener are in module java.desktop of loader 'bootstrap')
    at WAV2BIN.main(WAV2BIN.java:23)

Below is the current code I have for the project I'm working on:

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

public class WAV2BIN extends JFrame implements ActionListener {
    static JLabel label;

    public static void main(String[] args) {
        JFrame f = new JFrame("WAV2BIN");
        f.setSize(400, 200);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // "Browse" + "Convert" buttons
        JButton browse = new JButton("Browse...");
        JButton convert = new JButton("Convert");
        
        JFileChooser fileChooser = new JFileChooser();

        // add action listener to the buttons to capture user
        // response on buttons
        browse.addActionListener((ActionListener) fileChooser);
        convert.addActionListener((ActionListener) fileChooser);

        // make panel for buttons
        JPanel panel = new JPanel();

        // add buttons to frame
        panel.add(browse);
        panel.add(convert);

        // create new instance of 'label' object 
        label = new JLabel("No file selected.");
        panel.add(label);
        f.add(panel);

        f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
        // if user presses convert button, show convert dialog 
        String com = e.getActionCommand();

        if(com.equals("Convert")) {
            // create an object of JFileChooser class
            JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());

            // invoke showSaveDialog function to show save dialog
            int r = j.showSaveDialog(null);

            // if user selects a file
            if(r == JFileChooser.APPROVE_OPTION) {
                // set the label to the path of the selected file
                label.setText(j.getSelectedFile().getAbsolutePath());
            } else {
                label.setText("Operation cancelled.");
            }
        } else {
            JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());

            // invoke showOpenDialog to show the save dialog
            int r = j.showOpenDialog(null);

            // if the user selects a file
            if(r == JFileChooser.APPROVE_OPTION) {
                // set the label to the path of the selected file
                label.setText(j.getSelectedFile().getAbsolutePath());
            } else {
                label.setText("Operation cancelled.");
            }
        }
    }
}

My current issues are with the code shown above, as the buttons fail to show up and there is an Exception stating that

JFileChooser cannot be cast to class java.awt.event.ActionListener on line 23.

VSCode declared errors when I didn't cast fileChooser to ActionListener. Though I had errors, I did succeed in building WAV to BIN converter class, which I hope to assign to the "Convert" button in the finished application. Below is the finished product for that:

import java.io.*;
import java.lang.StringBuffer;
import java.util.Scanner;

public class Convert {
    private static String toBIN(int i) {
        String temp = "0000000" + Integer.toBinaryString(i);
        return temp.substring((temp.length() - 8)).toUpperCase();
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        System.out.print("Enter file name: ");
        String filename = input.next()+".wav()";
        StringBuffer sb = new StringBuffer();
        
        File file = new File(filename);
        BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
        for(int i; (i = is.read()) != -1;) {
            String binaryNumber = toBIN(i);
            sb.append(binaryNumber);
        }
        BufferedWriter br = new BufferedWriter(new FileWriter(new File(filename.substring(0, filename.length()-4)+".bin")));
        br.write(sb.toString());
        br.flush();
        br.close();
        input.close();
        is.close();
    }
}

I'm hoping someone can inform me of how to use the file name of the file chosen by the user as the input for the Convert class, as I understand that typing in the name of the file to be converted will no longer be necessary.

If you took time out of your day to read this, thank you. If you took even more time out to help me solve the issues with my code, I really appreciate it. I'm hoping this will help people who need it, as there's not really any programs on the web (at least from what I've seen) that do this.

0 Answers
Related