JavaScript Swing Application


/ Published in: JavaScript
Save to your folder(s)

A simple example of creating a Swing application using the JavaScript support provided by the Scripting API.


Copy this code and paste it in your HTML
  1. javax.swing.SwingUtilities.invokeLater(function() {
  2. var frame = new Packages.javax.swing.JFrame();
  3. frame.defaultCloseOperation = javax.swing.JFrame.EXIT_ON_CLOSE;
  4.  
  5. var menuBar = new Packages.javax.swing.JMenuBar();
  6. frame.setJMenuBar(menuBar);
  7.  
  8. var fileMenu = new Packages.javax.swing.JMenu("File");
  9. menuBar.add(fileMenu);
  10.  
  11. importClass(Packages.javax.swing.JMenuItem);
  12.  
  13. var openItem = new JMenuItem("Open...");
  14. openItem.addActionListener(function() {
  15. print("put your file open code here!\n");
  16. });
  17. fileMenu.add(openItem);
  18.  
  19. fileMenu.add(new Packages.javax.swing.JSeparator());
  20.  
  21. var quitItem = new JMenuItem("Quit");
  22. quitItem.addActionListener(function() {
  23. java.lang.System.exit(0);
  24. });
  25. fileMenu.add(quitItem);
  26.  
  27. frame.setSize(640, 480);
  28. frame.visible = true;
  29. });
  30.  
  31. // Park the main thread. Otherwise the application would immediately exit.
  32. java.util.concurrent.locks.LockSupport.park();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.