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