Skip to content

Commit db8615a

Browse files
author
Allan Jacobs
committed
Undecorated window case enabled in FrameDemo2. Removed all uses of peer comoponents.
1 parent 722a2a3 commit db8615a

File tree

1 file changed

+60
-50
lines changed
  • examples/src/main/scala/scala/swing/examples/tutorials/components

1 file changed

+60
-50
lines changed

examples/src/main/scala/scala/swing/examples/tutorials/components/FrameDemo2.scala

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,11 @@ class FrameDemo2 {
7777

7878
//Create a new MyFrame object and show it.
7979
def showNewWindow(): Unit = {
80-
val frame: Option[Frame] = if (noDecorations) None else Some(new MyFrame())
81-
8280
//Take care of the no window decorations case.
8381
//NOTE: Unless you really need the functionality
8482
//provided by JFrame, you would usually use a
8583
//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())
9585
//Set window location.
9686
if (frame.isDefined) {
9787
val f = frame.get
@@ -123,7 +113,7 @@ class FrameDemo2 {
123113
}
124114

125115
// Create the window-creation controls that go in the main window.
126-
def createOptionControls(frame: Frame): Box = {
116+
def createOptionControls(frame: Frame): BoxPanel = {
127117
val label1 = new Label("Decoration options for subsequently created frames:")
128118
val bg1 = new ButtonGroup()
129119
val label2 = new Label("Icon options:")
@@ -163,22 +153,20 @@ class FrameDemo2 {
163153
bg2.buttons += rb6
164154

165155
//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+
}
182170

183171
frame.listenTo(rb1)
184172
frame.listenTo(rb2)
@@ -195,13 +183,8 @@ class FrameDemo2 {
195183
JFrame.setDefaultLookAndFeelDecorated(false)
196184
case ButtonClicked(`rb3`) =>
197185
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-
//
202186
// No need to set the default look and feel decorated property to false.
203187
// JFrame.setDefaultLookAndFeelDecorated(false)
204-
println("Undecorated frames are not supported by Scala Swing.")
205188
case ButtonClicked(`rb4`) => specifyIcon = false
206189
case ButtonClicked(`rb5`) =>
207190
specifyIcon = true
@@ -215,7 +198,7 @@ class FrameDemo2 {
215198
}
216199

217200
//Create the button that goes in the main window.
218-
def createButtonPane(frame: Frame): javax.swing.JComponent = {
201+
def createButtonPane(frame: Frame): FlowPanel = {
219202
val button = new Button("New window")
220203
defaultButton = button
221204

@@ -229,7 +212,7 @@ class FrameDemo2 {
229212
border = Swing.EmptyBorder(5, 5, 5, 5)
230213
contents += button
231214
}
232-
pane.peer
215+
pane
233216
}
234217

235218
}
@@ -238,16 +221,45 @@ class MyFrame extends Frame {
238221
title = "A window"
239222

240223
//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+
}
242255

243256
//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+
}
251263
//
252264
listenTo(button)
253265
reactions += {
@@ -307,13 +319,11 @@ object FrameDemo2 extends SimpleSwingApplication {
307319
JDialog.setDefaultLookAndFeelDecorated(true);
308320
//Create and set up the content pane.
309321
val demo = new FrameDemo2();
310-
//Add components to it.
311-
val contentPane = peer.getContentPane()
312-
313322
}
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
318328
javax.swing.SwingUtilities.updateComponentTreeUI(top.peer)
319329
}

0 commit comments

Comments
 (0)