|
11 | 11 | import java.util.ArrayList; |
12 | 12 | import java.awt.*; |
13 | 13 | import java.net.URL; |
| 14 | +import java.util.prefs.Preferences; |
14 | 15 |
|
15 | 16 | import javax.swing.*; |
16 | 17 | import javax.swing.UIManager.LookAndFeelInfo; |
@@ -70,9 +71,27 @@ public void generateUI() { |
70 | 71 | * @param arg Whether or not to display |
71 | 72 | */ |
72 | 73 | public void showUI(boolean arg) { |
73 | | - // Pack, centre and show |
74 | | - frame.pack(); |
75 | | - frame.setLocationRelativeTo(null); |
| 74 | + if (arg) { |
| 75 | + Preferences prefs = Preferences.userNodeForPackage(GUI.class); |
| 76 | + if (prefs.getBoolean("window_stored", false)) { |
| 77 | + // Window border has been stored |
| 78 | + int window_x, window_y, window_width, window_height; |
| 79 | + window_x = prefs.getInt("window_x", 0); |
| 80 | + window_y = prefs.getInt("window_y", 0); |
| 81 | + window_height = prefs.getInt("window_height", 600); |
| 82 | + window_width = prefs.getInt("window_width", 800); |
| 83 | + frame.setBounds(window_x, window_y, window_width, window_height); |
| 84 | + // Restore maximise state |
| 85 | + if (prefs.getBoolean("window_maximised", false)) { |
| 86 | + frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH); |
| 87 | + } |
| 88 | + } |
| 89 | + else { |
| 90 | + // Pack & center the window |
| 91 | + frame.pack(); |
| 92 | + frame.setLocationRelativeTo(null); |
| 93 | + } |
| 94 | + } |
76 | 95 | frame.setVisible(arg); |
77 | 96 | } |
78 | 97 |
|
@@ -117,7 +136,26 @@ private void createFrame() { |
117 | 136 | frame.addWindowListener(new WindowAdapter() { |
118 | 137 | @Override |
119 | 138 | public void windowClosing(WindowEvent e) { |
| 139 | + // Prevents closing without saving first |
120 | 140 | if (checkSave()) { |
| 141 | + // Store window size/maximise state |
| 142 | + Preferences prefs = Preferences.userNodeForPackage(GUI.class); |
| 143 | + if ((frame.getExtendedState() & JFrame.MAXIMIZED_BOTH) != 0) { |
| 144 | + prefs.putBoolean("window_maximised", true); |
| 145 | + Rectangle windowBounds = e.getWindow().getBounds(); |
| 146 | + prefs.putInt("window_x", windowBounds.x + 50); |
| 147 | + prefs.putInt("window_y", windowBounds.y + 50); |
| 148 | + } |
| 149 | + else { |
| 150 | + prefs.putBoolean("window_maximised", false); |
| 151 | + Rectangle windowBounds = e.getWindow().getBounds(); |
| 152 | + prefs.putInt("window_x", windowBounds.x); |
| 153 | + prefs.putInt("window_y", windowBounds.y); |
| 154 | + prefs.putInt("window_width", windowBounds.width); |
| 155 | + prefs.putInt("window_height", windowBounds.height); |
| 156 | + } |
| 157 | + prefs.putBoolean("window_stored", true); |
| 158 | + // Close the window |
121 | 159 | e.getWindow().dispose(); |
122 | 160 | } |
123 | 161 | } |
@@ -181,6 +219,7 @@ private void createCompPane() { |
181 | 219 | JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); |
182 | 220 | sp.getVerticalScrollBar().setUnitIncrement(10); |
183 | 221 | sp.setPreferredSize(new Dimension(210,0)); |
| 222 | + sp.setMinimumSize(new Dimension(210, 60)); |
184 | 223 |
|
185 | 224 | hSplit.add(sp, JSplitPane.LEFT); |
186 | 225 | } |
|
0 commit comments