I am customizing an App for personal use. This App can make many midi files in Java. I want to choose and SAVE many files in one time from list as zip file.
There is a error message as below. How can I solve ClassCastException? Maybe it is mitake of Cast of JFileChooser.
(Error message)
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: camidion.chordhelper.midieditor.PlaylistTableModel$1 cannot be cast to javax.swing.JFileChooser
at camidion.chordhelper.midieditor.PlaylistTableModel$SelectedSequenceAction.setEnebledBySelection(PlaylistTableModel.java:188)
at camidion.chordhelper.midieditor.PlaylistTableModel$SelectedSequenceAction.init(PlaylistTableModel.java:195)
at camidion.chordhelper.midieditor.PlaylistTableModel$SelectedSequenceAction.<init>(PlaylistTableModel.java:177)
at camidion.chordhelper.midieditor.MidiSequenceEditorDialog$SequenceListTable$1.<init>(MidiSequenceEditorDialog.java:442)
at camidion.chordhelper.midieditor.MidiSequenceEditorDialog$SequenceListTable.<init>(MidiSequenceEditorDialog.java:444)
at camidion.chordhelper.midieditor.MidiSequenceEditorDialog.<init>(MidiSequenceEditorDialog.java:1182)
at camidion.chordhelper.ChordHelperApplet.init(ChordHelperApplet.java:477)
at camidion.chordhelper.MidiChordHelper$AppletFrame.<init>(MidiChordHelper.java:72)
at camidion.chordhelper.MidiChordHelper$1.run(MidiChordHelper.java:48)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
(My idea)
public ListSelectionModel sequenceListSelectionModel = new DefaultListSelectionModel() {
{
setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
};
-
From "protected void setEnebledBySelection() {....", error massage is in here.
public abstract class SelectedSequenceAction extends AbstractAction implements ListSelectionListener {
public SelectedSequenceAction(String name, Icon icon, String tooltip) {
super(name,icon); init(tooltip);
}
public SelectedSequenceAction(String name, String tooltip) {
super(name); init(tooltip);
}
@Override
public void valueChanged(ListSelectionEvent e) {
if( e.getValueIsAdjusting() ) return;
setEnebledBySelection();
}
protected void setEnebledBySelection() {
JFileChooser filechooser = new JFileChooser();
filechooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
filechooser.setMultiSelectionEnabled(true);
File[] index = ((JFileChooser) sequenceListSelectionModel).getSelectedFiles();
setEnabled(index != null);
}
private void init(String tooltip) {
putValue(Action.SHORT_DESCRIPTION, tooltip);
sequenceListSelectionModel.addListSelectionListener(this);
setEnebledBySelection();
}
}
-
/**
* This is about Action for files
*/
public Action saveMidiFileAction = getModel().new SelectedSequenceAction(
"Save",
"Save selected MIDI sequence to file"
) {
@Override
public void actionPerformed(ActionEvent event) {
PlaylistTableModel playlistModel = getModel();
SequenceTrackListTableModel sequenceModel = playlistModel.getSelectedSequenceModel();
String fn = sequenceModel.getFilename();
if( fn != null && ! fn.isEmpty() ) setSelectedFile(new File(fn));
if( showSaveDialog((Component)event.getSource()) != JFileChooser.APPROVE_OPTION ) return;
File[] f = getSelectedFiles();
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream("/Users/DANIEL/Downloads"));
byte[] buffer = new byte[1024];
int len;
}
for(File files : f) {
InputStream in = new FileInputStream(files);
out.putNextEntry(new ZipEntry(files.getName()));
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
}
out.close();
} catch(FileNotFoundException e){
e.printStackTrace();
showError(e.getMessage());
} catch(Exception e){
e.printStackTrace();
showError(e.getMessage());
}
}
};
(Original cord)
public ListSelectionModel sequenceListSelectionModel = new DefaultListSelectionModel() {
{
setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
};
-
public abstract class SelectedSequenceAction extends AbstractAction implements ListSelectionListener {
public SelectedSequenceAction(String name, Icon icon, String tooltip) {
super(name,icon); init(tooltip);
}
public SelectedSequenceAction(String name, String tooltip) {
super(name); init(tooltip);
}
@Override
public void valueChanged(ListSelectionEvent e) {
if( e.getValueIsAdjusting() ) return;
setEnebledBySelection();
}
protected void setEnebledBySelection() {
int index = sequenceListSelectionModel.getMinSelectionIndex();
setEnabled(index >= 0);
}
private void init(String tooltip) {
putValue(Action.SHORT_DESCRIPTION, tooltip);
sequenceListSelectionModel.addListSelectionListener(this);
setEnebledBySelection();
}
}
-
public Action saveMidiFileAction = getModel().new SelectedSequenceAction(
"Save",
"Save selected MIDI sequence to file"
) {
@Override
public void actionPerformed(ActionEvent event) {
PlaylistTableModel playlistModel = getModel();
SequenceTrackListTableModel sequenceModel = playlistModel.getSelectedSequenceModel();
String fn = sequenceModel.getFilename();
if( fn != null && ! fn.isEmpty() ) setSelectedFile(new File(fn));
if( showSaveDialog((Component)event.getSource()) != JFileChooser.APPROVE_OPTION ) return;
File f = getSelectedFile();
if( f.exists() ) {
fn = f.getName();
if( ! confirm("Overwrite " + fn + " ?\n" + fn + " is OK?") ) return;
}
try ( FileOutputStream out = new FileOutputStream(f) ) {
out.write(sequenceModel.getMIDIdata());
sequenceModel.setModified(false);
playlistModel.fireSequenceModified(sequenceModel, false);
}
catch( IOException ex ) {
ex.printStackTrace();
showError( ex.getMessage() );
}
}
};
ClassCastException usually occurs when you cast some object to a given type, but it won't have this type at runtime.
For example :
public void foo(Object o) {
((MyObject) o).someFunction();
}
Here, the code will compile since Object is the super class of all objects, but you need to be careful that your object will be typed as MyObject at runtime.
Edit:
You do :
File[] index = ((JFileChooser) sequenceListSelectionModel).getSelectedFiles();
Are you sure the sequenceListSelectionModel
can be cast to JFileChooser
?