1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| public class DemoSaveData {
public static final String FRAME_BOUNDS = "frameBounds";
public static final String FIELD_TEXT = "fieldText";
private JFrame frame;
private JTextField field;
public DemoSaveData() {
}
public void registerFrame(JFrame frame) {
this.frame=frame;
}
public void registerField(JTextField field) {
this.field=field;
}
private Path getApplicationDataFile(boolean create) throws IOException {
final Path applicationDataDir = Paths.get(System.getProperty("user.home"), "monApplication"); // un dossier pour stocker les données des l'application
if ( create && !Files.exists(applicationDataDir) ) { // création du dossier s'il n'existe pas
Files.createDirectories(applicationDataDir);
}
return applicationDataDir.resolve("monFichier.dat"); // retourne un nom de fichier pour stocker les info
}
public void save() {
try {
Path applicationDataFile = getApplicationDataFile(true);
// faire la sauvegarde des données
// par exemple, ici je sauvegarde la position de la frame et le texte dans le champ
try(BufferedWriter writer = Files.newBufferedWriter(applicationDataFile) ) {
writeData(writer, FRAME_BOUNDS, toStringData(frame.getBounds()));
writeData(writer, FIELD_TEXT, field.getText());
}
}
catch(IOException e) {
System.err.println("Sauvegarde impossible");
e.printStackTrace();
}
}
private static String toStringData(Rectangle bounds) {
return Arrays.stream(new int[]{bounds.x,bounds.y,bounds.width,bounds.height})
.mapToObj(String::valueOf)
.collect(Collectors.joining(","));
}
private static void writeData(BufferedWriter writer, String name, String value) throws IOException {
writer.write(name);
writer.write('=');
writer.write(value);
writer.newLine();
}
public void load() {
try {
Path applicationDataFile = getApplicationDataFile(false);
if ( Files.exists(applicationDataFile) ) {
for(String line : Files.readAllLines(applicationDataFile)) {
String[] data = line.split("=", 2);
if ( data.length==2 ) {
switch(data[0]) {
case FRAME_BOUNDS:
frame.setBounds(readBoundsData(data[1]));
break;
case FIELD_TEXT:
field.setText(data[1]);
break;
}
}
}
}
} catch (IOException e) {
System.err.println("Rechargement impossible");
e.printStackTrace();
}
}
private static Rectangle readBoundsData(String string) throws IOException {
try {
int[] bounds = Arrays.stream(string.split(","))
.mapToInt(Integer::parseInt)
.toArray();
if ( bounds.length!=4 ) {
throw new IOException("Not enough data: " + bounds.length + " instead of 4.");
}
return new Rectangle(bounds[0], bounds[1], bounds[2], bounds[3]);
}
catch(NumberFormatException e) {
throw new IOException(e);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JTextField textField = new JTextField();
frame.add(textField, BorderLayout.SOUTH);
DemoSaveData saveData = new DemoSaveData();
saveData.registerFrame(frame);
saveData.registerField(textField);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
saveData.save(); // on sauvegarde les données quand on ferme la fenêtre
}
});
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
saveData.load();
frame.setVisible(true);
}
} |
Partager