~ubuntu-branches/ubuntu/karmic/rhino/karmic

« back to all changes in this revision

Viewing changes to examples/SwingApplication.js

  • Committer: Bazaar Package Importer
  • Author(s): Jerry Haltom
  • Date: 2005-03-19 16:56:07 UTC
  • mto: (11.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050319165607-geu3j3fnqlkpqkh1
Tags: upstream-1.6.R1
ImportĀ upstreamĀ versionĀ 1.6.R1

Show diffs side-by-side

added added

removed removed

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