Skip to content

Commit cbc70f9

Browse files
author
Allan Jacobs
committed
FlowLayout, GridLayout, GridBagLayout demos scalified.
1 parent f0ea41e commit cbc70f9

File tree

4 files changed

+239
-8
lines changed

4 files changed

+239
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ build.properties
3434
/.settings
3535
/examples/.classpath
3636
/examples/.project
37+
scala-swing.iml
3738

3839
# bak files produced by ./cleanup-commit
3940
*.bak

examples/src/main/scala/scala/swing/examples/tutorials/layout/FlowLayoutDemo.scala

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,62 @@
3131
package scala.swing.examples.tutorials.layout
3232

3333
import scala.swing._
34+
import scala.swing.event.ButtonClicked
35+
import java.awt.ComponentOrientation
3436
import javax.swing.UIManager
3537

3638
/**
37-
* Tutorials: Creating a Custom Layout Manager
38-
* [[http://docs.oracle.com/javase/tutorial/uiswing/layout/custom.html]]
39+
* Tutorials: How to Use FlowLayout
40+
* [[http://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html]]
3941
*
4042
* Source code reference:
41-
* [[http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/CustomLayoutDemoProject/src/layout/CustomLayoutDemo.java]]
42-
* [[http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/CustomLayoutDemoProject/src/layout/DiagonalLayout.java]]
43+
* [[http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/FlowLayoutDemoProject/src/layout/FlowLayoutDemo.java]]
4344
*
44-
* CustomLayoutDemo.scala requires one other file:
45-
* DiagonalLayout.scala
45+
* FlowLayoutDemo.scala requires no other files
4646
*/
47-
class FlowLayoutDemo() extends FlowPanel {
48-
47+
class FlowLayoutDemo extends BorderPanel {
48+
val RtoL = "Right to left"
49+
val LtoR = "Left to right"
50+
val controls: FlowPanel = new FlowPanel()
51+
val applyButton = new Button("Apply component orientation")
52+
val compsToExperiment = new FlowPanel(FlowPanel.Alignment.Trailing)()
53+
val LtoRbutton = new RadioButton(LtoR) { selected = true }
54+
val RtoLbutton = new RadioButton(RtoL)
55+
56+
//Add buttons to the experiment layout
57+
compsToExperiment.contents += new Button("Button 1")
58+
compsToExperiment.contents += new Button("Button 2")
59+
compsToExperiment.contents += new Button("Button 3")
60+
compsToExperiment.contents += new Button("Long Named Button 4")
61+
compsToExperiment.contents += new Button("5")
62+
63+
//Left to right component orientation is selected by default
64+
compsToExperiment.peer.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT)
65+
66+
//Add controls to set up the component orientation in the experiment layout
67+
val group = new ButtonGroup() {
68+
buttons += LtoRbutton
69+
buttons += RtoLbutton
70+
}
71+
controls.contents += LtoRbutton
72+
controls.contents += RtoLbutton
73+
controls.contents += applyButton
74+
75+
layout(compsToExperiment) = BorderPanel.Position.Center
76+
layout(controls) = BorderPanel.Position.South
77+
78+
listenTo(applyButton)
79+
reactions += {
80+
case ButtonClicked(`applyButton`) =>
81+
if (LtoRbutton.selected) {
82+
compsToExperiment.peer.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT)
83+
}
84+
else {
85+
compsToExperiment.peer.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT)
86+
}
87+
compsToExperiment.peer.validate()
88+
compsToExperiment.peer.repaint()
89+
}
4990
}
5091

5192
object FlowLayoutDemo extends SimpleSwingApplication {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of Oracle or the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
package scala.swing.examples.tutorials.layout
32+
33+
import scala.swing._
34+
35+
/**
36+
* Tutorials: How to Use GridBagLayout
37+
* [[http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html]]
38+
*
39+
* Source code reference:
40+
* [[http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/GridBagLayoutDemoProject/src/layout/GridBagLayoutDemo.java]]
41+
*
42+
* GridBagLayoutDemo.scala requires no other files.
43+
*/
44+
class GridBagLayoutDemo extends GridBagPanel {
45+
val shouldFill = true
46+
val shouldWeightX = true
47+
val RightToLeft = false
48+
val c: Constraints = new Constraints()
49+
//natural height, maximum width
50+
if (shouldFill) c.fill = GridBagPanel.Fill.Horizontal
51+
if (shouldWeightX) c.weightx = 0.5
52+
c.gridx = 0
53+
c.gridy = 0
54+
layout(new Button("Button 1")) = c
55+
56+
c.fill = GridBagPanel.Fill.Horizontal
57+
c.weightx = 0.5
58+
c.gridx = 1
59+
c.gridy = 0
60+
layout(new Button("Button 2")) = c
61+
62+
c.fill = GridBagPanel.Fill.Horizontal
63+
c.weightx = 0.5
64+
c.gridx = 2
65+
c.gridy = 0
66+
layout(new Button("Button 3")) = c
67+
68+
c.fill = GridBagPanel.Fill.Horizontal
69+
c.ipady = 40 // make this component tall
70+
c.weightx = 0.5
71+
c.gridwidth = 3
72+
c.gridx = 0
73+
c.gridy = 1
74+
layout(new Button("Long-Named Button 4")) = c
75+
76+
c.fill = GridBagPanel.Fill.Horizontal
77+
c.ipady = 0 //reset to default
78+
c.weightx = 1.0 //request any extra vertical space
79+
c.anchor = GridBagPanel.Anchor.PageEnd //bottom of space
80+
c.insets = new Insets(10, 0, 0, 0) // top padding
81+
c.gridx = 1
82+
c.gridwidth = 2
83+
c.gridy = 22
84+
layout(new Button("5")) = c
85+
}
86+
87+
object GridBagLayoutDemo extends SimpleSwingApplication {
88+
lazy val top = new MainFrame() {
89+
title = "GridBagLayoutDemo"
90+
contents = new GridBagLayoutDemo()
91+
}
92+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of Oracle or the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
package scala.swing.examples.tutorials.layout
32+
33+
import scala.swing._
34+
import scala.swing.event.ButtonClicked
35+
import javax.swing.UIManager
36+
import java.awt.Dimension
37+
38+
/**
39+
* Tutorials: How to Use GridLayout
40+
* [[http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html]]
41+
*
42+
* Source code reference:
43+
* [[http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/GridLayoutDemoProject/src/layout/GridLayoutDemo.java]]
44+
*/
45+
class GridLayoutDemo extends BorderPanel {
46+
private val gapList = Array("0", "10", "15", "20")
47+
private val maxGap = 20
48+
val horGapComboBox = new ComboBox(gapList)
49+
val verGapComboBox = new ComboBox(gapList)
50+
val applyButton = new Button("Apply gaps")
51+
val compsToExperiment = new GridPanel(0, 2)
52+
val controls = new GridPanel(2, 3)
53+
//Set up components preferred size
54+
val b = new Button("Just fake button")
55+
val buttonSize = b.preferredSize
56+
compsToExperiment.preferredSize =
57+
new Dimension((buttonSize.getWidth() * 2.5).toInt + maxGap,
58+
(buttonSize.getHeight() * 3.5).toInt + maxGap * 2)
59+
60+
//Add buttons to experiment with Grid Layout
61+
compsToExperiment.contents += new Label("Horizontal gap:")
62+
compsToExperiment.contents += new Label("Vertical gap:")
63+
compsToExperiment.contents += new Label(" ")
64+
compsToExperiment.contents += horGapComboBox
65+
compsToExperiment.contents += verGapComboBox
66+
compsToExperiment.contents += applyButton
67+
layout(compsToExperiment) = BorderPanel.Position.North
68+
layout(controls) = BorderPanel.Position.South
69+
70+
listenTo(applyButton)
71+
reactions += {
72+
case ButtonClicked(`applyButton`) =>
73+
//Get the horizontal gap value
74+
val horGap = horGapComboBox.selection.item
75+
//Get the vertical gap value
76+
val verGap = verGapComboBox.selection.item
77+
//Set up the horizontal gap value
78+
compsToExperiment.hGap = horGap.toInt
79+
//Set up the vertical gap value
80+
compsToExperiment.vGap = verGap.toInt
81+
//Set up the layout of the buttons
82+
compsToExperiment.peer.getLayout().layoutContainer(compsToExperiment.peer)
83+
}
84+
}
85+
86+
object GridLayoutDemo extends SimpleSwingApplication {
87+
/* Use an appropriate Look and Feel */
88+
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel")
89+
/* Turn off metal's use bold fonts */
90+
UIManager.put("swing.boldMetal", false)
91+
//Create and set up the window.
92+
lazy val top = new MainFrame {
93+
title = "GridLayoutDemo"
94+
resizable = false
95+
contents = new GridLayoutDemo()
96+
}
97+
}

0 commit comments

Comments
 (0)