Skip to content

Commit 8c0754a

Browse files
author
Allan Jacobs
committed
Events demos Beeper and ListSelectionDemo. Fixed the orientation of the
splitpanes used in SplitPaneDemo, SplitPaneDemo2, and SplitPaneDividerDemo.
1 parent cbc70f9 commit 8c0754a

File tree

5 files changed

+210
-12
lines changed

5 files changed

+210
-12
lines changed

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,7 @@ import java.awt.Font
3737
import java.net.URL
3838
import javax.swing.ImageIcon
3939

40-
/**
41-
* Tutorial: How to Use Split Panes
42-
* [[http://docs.oracle.com/javase/tutorial/uiswing/components/splitpane.html]]
43-
*
44-
* Source code reference:
45-
* [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/SplitPaneDemoProject/src/components/SplitPaneDemo.java]]
46-
*/
47-
//SplitPaneDemo itself is not a visible component.
40+
//SplitPaneDemo itself is not a visible component. It is used by SplitPaneDemo2.scala.
4841
class SplitPaneDemo extends FlowPanel {
4942
val imageNames: Array[String] = Array("Bird", "Cat", "Dog", "Rabbit", "Pig",
5043
"dukeWaveRed", "kathyCosmo", "left", "middle", "right", "stickerface")
@@ -61,7 +54,8 @@ class SplitPaneDemo extends FlowPanel {
6154
val pictureScrollPane = new ScrollPane(picture)
6255

6356
//Create a split pane with the two scroll panes in it.
64-
val splitPane = new SplitPane(Orientation.Horizontal, listScrollPane, pictureScrollPane) {
57+
//Use Orientation.Vertical to get a left/right split pane
58+
val splitPane = new SplitPane(Orientation.Vertical, listScrollPane, pictureScrollPane) {
6559
oneTouchExpandable = true
6660
dividerLocation = 150
6761
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ import javax.swing.ImageIcon
4242
*
4343
* Source code reference:
4444
* [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/SplitPaneDemo2Project/src/components/SplitPaneDemo2.java]]
45+
* [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/SplitPaneDemoProject/src/components/SplitPaneDemo.java]]
46+
*
47+
* SplitPaneDemo2.scala uses
48+
* SplitPaneDemo.scala
4549
*/
4650
class SplitPaneDemo2 extends MainFrame {
4751
title = "SplitPaneDemo2"
@@ -63,8 +67,9 @@ class SplitPaneDemo2 extends MainFrame {
6367
}
6468

6569
//Create a split pane and put "top" (a split pane)
66-
//and JLabel instance in it.
67-
val splitPane = new SplitPane(Orientation.Vertical, top, label) {
70+
//and JLabel instance in it.
71+
//Use Orientation.Horizontal to get a top/bottom split pane.
72+
val splitPane = new SplitPane(Orientation.Horizontal, top, label) {
6873
oneTouchExpandable = true
6974
dividerLocation = 180
7075
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class SplitPaneDividerDemo extends BorderPanel {
6464
}
6565
sd2.setMinimumSize(new Dimension(30,30))
6666

67-
val splitPane = new SplitPane(Orientation.Horizontal, sd1, sd2) {
67+
val splitPane = new SplitPane(Orientation.Vertical, sd1, sd2) {
6868
resizeWeight = 0.5
6969
oneTouchExpandable = true
7070
continuousLayout = true
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.events
32+
33+
import scala.swing._
34+
import scala.swing.event.ButtonClicked
35+
import java.awt.{Dimension,Toolkit }
36+
37+
/**
38+
* Tutorial: Introduction to Event Listeners
39+
* [[http://docs.oracle.com/javase/tutorial/uiswing/events/intro.html]]
40+
*
41+
* Source code reference:
42+
* [[http://docs.oracle.com/javase/tutorial/uiswing/examples/events/BeeperProject/src/events/Beeper.java]]
43+
*
44+
* Beeper.scala requires no other files.
45+
*/
46+
class Beeper extends BorderPanel {
47+
val button = new Button("Click Me") {
48+
preferredSize = new Dimension(200, 80)
49+
}
50+
layout(button) = BorderPanel.Position.Center
51+
listenTo(button)
52+
reactions += {
53+
case ButtonClicked(`button`) =>
54+
Toolkit.getDefaultToolkit().beep()
55+
}
56+
57+
}
58+
59+
object Beeper extends SimpleSwingApplication {
60+
//Create and set up the window.
61+
lazy val top = new MainFrame {
62+
title = "Beeper"
63+
contents = new Beeper()
64+
}
65+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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.events
32+
33+
import scala.swing._
34+
import scala.swing.event.{ButtonClicked, SelectionChanged}
35+
import java.awt.Dimension
36+
37+
/**
38+
* Tutorial: How to Write a List Selection Listener
39+
* [[http://docs.oracle.com/javase/tutorial/uiswing/events/listselectionlistener.html]]
40+
*
41+
* Source code reference:
42+
* [[http://docs.oracle.com/javase/tutorial/uiswing/examples/events/ListSelectionDemoProject/src/events/ListSelectionDemo.java]]
43+
*
44+
* ListSelectionDemo.scala requires no other files.
45+
*/
46+
class ListSelectionDemo extends BorderPanel {
47+
val listData = Array( "one", "two", "three", "four",
48+
"five", "six", "seven" )
49+
val columnNames = Array( "French", "Spanish", "Italian" )
50+
val list = new ListView(listData)
51+
val listPane = new ScrollPane(list)
52+
53+
val modes = Array( "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION",
54+
"MULTIPLE_INTERVAL_SELECTION")
55+
val comboBox = new ComboBox(modes) {
56+
selection.index = 2
57+
}
58+
val controlPane = new FlowPanel() {
59+
contents += new Label("Selection mode:")
60+
contents += comboBox
61+
}
62+
63+
//Build output area.
64+
val output = new TextArea(1, 10) {
65+
editable = false
66+
}
67+
val outputPane = new ScrollPane(output) {
68+
verticalScrollBarPolicy = ScrollPane.BarPolicy.Always
69+
horizontalScrollBarPolicy = ScrollPane.BarPolicy.AsNeeded
70+
}
71+
val listContainer = new GridPanel(1, 1) {
72+
contents += listPane
73+
border = Swing.TitledBorder(Swing.EmptyBorder, "List")
74+
}
75+
val topHalf = new BoxPanel(Orientation.Horizontal) {
76+
contents += listContainer
77+
border = Swing.EmptyBorder(5, 5, 0, 5)
78+
minimumSize = new Dimension(100, 50)
79+
preferredSize = new Dimension(100, 110)
80+
}
81+
82+
val bottomHalf = new BorderPanel() {
83+
layout(controlPane) = BorderPanel.Position.North
84+
layout(outputPane) = BorderPanel.Position.Center
85+
preferredSize = new Dimension(450, 135)
86+
}
87+
88+
//Do the layout.
89+
val splitPane = new SplitPane(Orientation.Horizontal, topHalf, bottomHalf)
90+
91+
layout(splitPane) = BorderPanel.Position.Center
92+
93+
listenTo(comboBox.selection)
94+
listenTo(list.selection)
95+
96+
reactions += {
97+
case SelectionChanged(`comboBox`) =>
98+
val newMode = comboBox.selection.item
99+
newMode match {
100+
case "SINGLE_SELECTION" =>
101+
list.selection.intervalMode = ListView.IntervalMode.Single
102+
case "SINGLE_INTERVAL_SELECTION" =>
103+
list.selection.intervalMode = ListView.IntervalMode.SingleInterval
104+
case "MULTIPLE_INTERVAL_SELECTION" =>
105+
list.selection.intervalMode = ListView.IntervalMode.MultiInterval
106+
}
107+
output.append("----------" + "Mode: " + newMode + "----------" + "\n")
108+
case SelectionChanged(`list`) =>
109+
val firstIndex = list.selection.leadIndex
110+
val lastIndex = list.selection.anchorIndex // anchor = last?
111+
val isAdjusting = list.selection.adjusting
112+
output.append("Event for indexes " + firstIndex + " - " + lastIndex +
113+
"; isAdjusting is " + isAdjusting + "; selected indexes:")
114+
if (list.selection.indices.isEmpty) {
115+
output.append(" <none>")
116+
}
117+
else {
118+
// Find out which indexes are selected.
119+
for (i <- list.selection.indices) {
120+
output.append(" " + i)
121+
}
122+
}
123+
output.append("\n")
124+
output.caret.position = output.peer.getDocument().getLength()
125+
}
126+
}
127+
128+
object ListSelection extends SimpleSwingApplication {
129+
//Create and set up the window.
130+
lazy val top = new MainFrame {
131+
title = "ListSelectionDemo"
132+
contents = new ListSelectionDemo()
133+
}
134+
}

0 commit comments

Comments
 (0)