-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreferencesTest.java
More file actions
114 lines (99 loc) · 3.79 KB
/
PreferencesTest.java
File metadata and controls
114 lines (99 loc) · 3.79 KB
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
package preferences;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.io.*;
import java.util.prefs.Preferences;
/**
* A program to test preference settings. The program remembers the frame
* position, size, and title.
*
* @author Cay Horstmann
* @version 1.03 2015-06-12
*/
public class PreferencesTest {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
PreferencesFrame frame = new PreferencesFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
/**
* A frame that restores position and size from user preferences and updates the
* preferences upon exit.
*/
class PreferencesFrame extends JFrame {
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
private Preferences root = Preferences.userRoot();
private Preferences node = root.node("/");
public PreferencesFrame() {
// get position, size, title from preferences
int left = node.getInt("left", 0);
int top = node.getInt("top", 0);
int width = node.getInt("width", DEFAULT_WIDTH);
int height = node.getInt("height", DEFAULT_HEIGHT);
setBounds(left, top, width, height);
// if no title given, ask user
String title = node.get("title", "");
if (title.equals(""))
title = JOptionPane.showInputDialog("Please supply a frame title:");
if (title == null) title = "";
setTitle(title);
// set up file chooser that shows XML files
final JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new FileNameExtensionFilter("XML files", "xml"));
// set up menus
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem exportItem = new JMenuItem("Export preferences");
menu.add(exportItem);
exportItem
.addActionListener(event -> {
if (chooser.showSaveDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION) {
try {
savePreferences();
OutputStream out = new FileOutputStream(chooser
.getSelectedFile());
node.exportSubtree(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
JMenuItem importItem = new JMenuItem("Import preferences");
menu.add(importItem);
importItem
.addActionListener(event -> {
if (chooser.showOpenDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION) {
try {
InputStream in = new FileInputStream(chooser
.getSelectedFile());
Preferences.importPreferences(in);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
JMenuItem exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(event -> {
savePreferences();
System.exit(0);
});
}
public void savePreferences() {
node.putInt("left", getX());
node.putInt("top", getY());
node.putInt("width", getWidth());
node.putInt("height", getHeight());
node.put("title", getTitle());
}
}