[英]How can I save multiple files at the same time in Jfilechooser from JList?
How can I save multiple files at the same time in Jfilechooser from JList? 如何从JList在Jfilechooser中同时保存多个文件?
Now I could only one save a MIDI file that I selected from List. 现在,我只能保存从列表中选择的MIDI文件。
I already searched in Google long time. 我已经在Google中搜索了很长时间。 I don't have good idea.
我没有好主意。
-Change a "get model()" to something. -将“ get model()”更改为某些内容。
-Use a Zip system. -使用Zip系统。
-Select All files. -选择所有文件。
-Repeat a save action. -重复保存动作。
private class MidiFileChooser extends JFileChooser {
{
setFileFilter(new FileNameExtensionFilter("MIDI sequence (*.mid)", "mid"));
}
/**
* File Save Action
*/
public Action saveMidiFileAction = getModel().new SelectedSequenceAction(
"Save",
"Save selected MIDI sequence to file - Save a selected MIDI flies"
) {
@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() );
}
}
};
I explain details od my changes about File[] files = getSelectedFiles(); 我将解释有关File [] files = getSelectedFiles();的更改的详细信息。
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;
JFileChooser filechooser = new JFileChooser();
filechooser.setMultiSelectionEnabled(true);
File[] files = getSelectedFiles();
String timerecord = String.valueOf(Math.random());
try ( FileOutputStream out = new FileOutputStream(timerecord + ".mid") ) {
out.write(sequenceModel.getMIDIdata());
sequenceModel.setModified(false);
playlistModel.fireSequenceModified(sequenceModel, false);
}
catch( IOException ex ) {
ex.printStackTrace();
showError( ex.getMessage() );
}
}
};
this is Jfilechooser.class 这是Jfilechooser.class
public void setMultiSelectionEnabled(boolean b) {
if(multiSelectionEnabled == b) {
return;
}
boolean oldValue = multiSelectionEnabled;
multiSelectionEnabled = b;
firePropertyChange(MULTI_SELECTION_ENABLED_CHANGED_PROPERTY, oldValue, multiSelectionEnabled);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.