Debian Codegraph @

rhino/examples/SwingApplication.js

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 /*
6 * SwingApplication.js - a translation into JavaScript of
7 * SwingApplication.java, a java.sun.com Swing example.
8 *
9 * @author Roger E Critchlow, Jr.
10 */
11
12 var swingNames = JavaImporter();
13
14 swingNames.importPackage(Packages.javax.swing);
15 swingNames.importPackage(Packages.java.awt);
16 swingNames.importPackage(Packages.java.awt.event);
17
18 function createComponents()
19 {
20 with (swingNames) {
21 var labelPrefix = "Number of button clicks: ";
22 var numClicks = 0;
23 var label = new JLabel(labelPrefix + numClicks);
24 var button = new JButton("I'm a Swing button!");
25 button.mnemonic = KeyEvent.VK_I;
26 // Since Rhino 1.5R5 JS functions can be passed to Java method if
27 // corresponding argument type is Java interface with single method
28 // or all its methods have the same number of arguments and the
29 // corresponding arguments has the same type. See also comments for
30 // frame.addWindowListener bellow
31 button.addActionListener(function() {
32 numClicks += 1;
33 label.setText(labelPrefix + numClicks);
34 });
35 label.setLabelFor(button);
36
37 /*
38 * An easy way to put space between a top-level container
39 * and its contents is to put the contents in a JPanel
40 * that has an "empty" border.
41 */
42 var pane = new JPanel();
43 pane.border = BorderFactory.createEmptyBorder(30, //top
44 30, //left
45 10, //bottom
46 30); //right
47 pane.setLayout(new GridLayout(0, 1));
48 pane.add(button);
49 pane.add(label);
50
51 return pane;
52 }
53 }
54
55 with (swingNames) {
56 try {
57 UIManager.
58 setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
59 } catch (e) { }
60
61 //Create the top-level container and add contents to it.
62 var frame = new swingNames.JFrame("SwingApplication");
63 frame.getContentPane().add(createComponents(), BorderLayout.CENTER);
64
65 // Pass JS function as implementation of WindowListener. It is allowed since
66 // all methods in WindowListener have the same signature. To distinguish
67 // between methods Rhino passes to JS function the name of corresponding
68 // method as the last argument
69 frame.addWindowListener(function(event, methodName) {
70 if (methodName == "windowClosing") {
71 java.lang.System.exit(0);
72 }
73 });
74
75 //Finish setting up the frame, and show it.
76 frame.pack();
77 frame.setVisible(true);
78 }
79
80
81