Last active
March 25, 2018 08:34
-
-
Save romanitalian/d6df0a0dab4e001d9386c47889dcea11 to your computer and use it in GitHub Desktop.
How to restore and save melodi from/into file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Restore a melody from the file | |
*/ | |
private class RestoreMelodiListener implements ActionListener { | |
@Override | |
public void actionPerformed(ActionEvent a) { | |
boolean[] checkboxState = null; | |
try { | |
JFileChooser fin = new JFileChooser(); | |
fin.showOpenDialog(theFrame); | |
File selectedFile = fin.getSelectedFile(); | |
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(selectedFile)); | |
checkboxState = (boolean[]) ois.readObject(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
for (JCheckBox check : checkboxList) { | |
check.setSelected(check.isSelected()); | |
} | |
for (int i = 0; i < 256; i++) { | |
JCheckBox check = checkboxList.get(i); | |
check.setSelected(checkboxState != null && checkboxState[i]); | |
} | |
sequencer.stop(); | |
buildTrackAndStart(); | |
} | |
} | |
/** | |
* Save ringtone to file | |
*/ | |
private class SaveMelodiListener implements ActionListener { | |
@Override | |
public void actionPerformed(ActionEvent a) { | |
boolean[] checkboxState = new boolean[256]; | |
for (int i = 0; i < 256; i++) { | |
JCheckBox check = checkboxList.get(i); | |
if (check.isSelected()) { | |
checkboxState[i] = true; | |
} | |
} | |
try { | |
JFileChooser fin = new JFileChooser(); | |
fin.showOpenDialog(theFrame); | |
File selectedFile = fin.getSelectedFile(); | |
if (selectedFile != null) { | |
FileOutputStream fileStream = new FileOutputStream(selectedFile); | |
ObjectOutputStream os = new ObjectOutputStream(fileStream); | |
os.writeObject(checkboxState); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment