@@ -77,21 +77,11 @@ class FrameDemo2 {
77
77
78
78
// Create a new MyFrame object and show it.
79
79
def showNewWindow (): Unit = {
80
- val frame : Option [Frame ] = if (noDecorations) None else Some (new MyFrame ())
81
-
82
80
// Take care of the no window decorations case.
83
81
// NOTE: Unless you really need the functionality
84
82
// provided by JFrame, you would usually use a
85
83
// Window or JWindow instead of an undecorated JFrame.
86
-
87
- // Undecorated frames are not supported by scala swing. Setting the Frame's
88
- // JFrame peer to be undecorated causes Swing to throw a java.awt.IllegalComponentStateException
89
- // with the message "The frame is displayable."
90
- //
91
- // if (noDecorations) {
92
- // frame.peer.setUndecorated(true);
93
- // }
94
-
84
+ val frame : Option [Frame ] = if (noDecorations) Some (new MyFrameUndecorated ()) else Some (new MyFrame ())
95
85
// Set window location.
96
86
if (frame.isDefined) {
97
87
val f = frame.get
@@ -123,7 +113,7 @@ class FrameDemo2 {
123
113
}
124
114
125
115
// Create the window-creation controls that go in the main window.
126
- def createOptionControls (frame : Frame ): Box = {
116
+ def createOptionControls (frame : Frame ): BoxPanel = {
127
117
val label1 = new Label (" Decoration options for subsequently created frames:" )
128
118
val bg1 = new ButtonGroup ()
129
119
val label2 = new Label (" Icon options:" )
@@ -163,22 +153,20 @@ class FrameDemo2 {
163
153
bg2.buttons += rb6
164
154
165
155
// Add everything to a container.
166
- val box = Box .createVerticalBox()
167
- box.add(label1.peer)
168
- box.add(Swing .VStrut (5 ).peer) // spacer
169
- box.add(rb1.peer)
170
- box.add(rb2.peer)
171
- box.add(rb3.peer)
172
- //
173
- box.add(Swing .VStrut (15 ).peer) // spacer
174
- box.add(label2.peer)
175
- box.add(Swing .VStrut (5 ).peer) // spacer
176
- box.add(rb4.peer)
177
- box.add(rb5.peer)
178
- box.add(rb6.peer)
179
-
180
- // Add some breathing room.
181
- box.setBorder(Swing .EmptyBorder (10 , 10 , 10 , 10 ))
156
+ val box = new BoxPanel (Orientation .Vertical ) {
157
+ border = Swing .EmptyBorder (10 , 10 , 10 , 10 )
158
+ contents += label1
159
+ contents += Swing .VStrut (5 ) // spacer
160
+ contents += rb1
161
+ contents += rb2
162
+ contents += rb3
163
+ contents += Swing .VStrut (15 )
164
+ contents += label2
165
+ contents += Swing .VStrut (5 )
166
+ contents += rb4
167
+ contents += rb5
168
+ contents += rb6
169
+ }
182
170
183
171
frame.listenTo(rb1)
184
172
frame.listenTo(rb2)
@@ -195,13 +183,8 @@ class FrameDemo2 {
195
183
JFrame .setDefaultLookAndFeelDecorated(false )
196
184
case ButtonClicked (`rb3`) =>
197
185
noDecorations = true
198
- // Undecorated frames are not supported by scala swing. Setting the Frame's
199
- // JFrame peer to be undecorated causes Swing to throw a java.awt.IllegalComponentStateException
200
- // with the message "The frame is displayable."
201
- //
202
186
// No need to set the default look and feel decorated property to false.
203
187
// JFrame.setDefaultLookAndFeelDecorated(false)
204
- println(" Undecorated frames are not supported by Scala Swing." )
205
188
case ButtonClicked (`rb4`) => specifyIcon = false
206
189
case ButtonClicked (`rb5`) =>
207
190
specifyIcon = true
@@ -215,7 +198,7 @@ class FrameDemo2 {
215
198
}
216
199
217
200
// Create the button that goes in the main window.
218
- def createButtonPane (frame : Frame ): javax.swing. JComponent = {
201
+ def createButtonPane (frame : Frame ): FlowPanel = {
219
202
val button = new Button (" New window" )
220
203
defaultButton = button
221
204
@@ -229,7 +212,7 @@ class FrameDemo2 {
229
212
border = Swing .EmptyBorder (5 , 5 , 5 , 5 )
230
213
contents += button
231
214
}
232
- pane.peer
215
+ pane
233
216
}
234
217
235
218
}
@@ -238,16 +221,45 @@ class MyFrame extends Frame {
238
221
title = " A window"
239
222
240
223
// This button lets you close even an undecorated window.
241
- val button = new Button (" Close window" )
224
+ val button = new Button (" Close window" ) {
225
+ xLayoutAlignment = java.awt.Component .CENTER_ALIGNMENT
226
+ }
227
+
228
+ // Place the button near the bottom of the window.
229
+ contents = new BoxPanel (Orientation .Vertical ) {
230
+ contents += Swing .VGlue
231
+ contents += button
232
+ contents += Swing .VStrut (5 )
233
+ }
234
+ //
235
+ listenTo(button)
236
+ reactions += {
237
+ case ButtonClicked (`button`) =>
238
+ visible = false
239
+ dispose()
240
+ }
241
+ preferredSize = new Dimension (150 , 150 )
242
+ pack()
243
+ visible = true
244
+ override def closeOperation () = {
245
+ close
246
+ }
247
+ }
248
+
249
+ class MyFrameUndecorated extends Frame with RichWindow .Undecorated {
250
+ visible = false
251
+ // This button lets you close even an undecorated window.
252
+ val button = new Button (" Close window" ) {
253
+ xLayoutAlignment = java.awt.Component .CENTER_ALIGNMENT
254
+ }
242
255
243
256
// Place the button near the bottom of the window.
244
- // Undecorated windows are not supported in scala swing. Go to the peer.
245
- val contentPane : java.awt.Container = peer.getContentPane()
246
- contentPane.setLayout(new BoxLayout (contentPane, BoxLayout .PAGE_AXIS ))
247
- contentPane.add(Box .createVerticalGlue()) // takes all extra space
248
- contentPane.add(button.peer)
249
- button.xLayoutAlignment = java.awt.Component .CENTER_ALIGNMENT
250
- contentPane.add(Box .createVerticalStrut(5 )) // spacer
257
+ // Undecorated windows are not supported in scala swing.
258
+ contents = new BoxPanel (Orientation .Vertical ) {
259
+ contents += Swing .VGlue
260
+ contents += button
261
+ contents += Swing .VStrut (5 )
262
+ }
251
263
//
252
264
listenTo(button)
253
265
reactions += {
@@ -307,13 +319,11 @@ object FrameDemo2 extends SimpleSwingApplication {
307
319
JDialog .setDefaultLookAndFeelDecorated(true );
308
320
// Create and set up the content pane.
309
321
val demo = new FrameDemo2 ();
310
- // Add components to it.
311
- val contentPane = peer.getContentPane()
312
-
313
322
}
314
- top.contentPane.add(top.demo.createOptionControls(top),
315
- BorderLayout .CENTER )
316
- top.contentPane.add(top.demo.createButtonPane(top),
317
- BorderLayout .PAGE_END )
323
+ val bp : BorderPanel = new BorderPanel () {
324
+ layout(top.demo.createOptionControls(top)) = BorderPanel .Position .Center
325
+ layout(top.demo.createButtonPane(top)) = BorderPanel .Position .South
326
+ }
327
+ top.contents = bp
318
328
javax.swing.SwingUtilities .updateComponentTreeUI(top.peer)
319
329
}
0 commit comments