API-icon.png

Unknown macro: floating-toc

Scripting in NetBeans

BeanShell Script File Development with NetBeans

The NetBeans software is available as download on the  NetBeans Website. We provide a sample project for Script Files created with NetBeans. For detailed information have a look at chapter 1.2.2.3 First, after installing NetBeans IDE a new project has to be created or loaded. If a project is loaded, go to third to continue.

beanshell_netbeans.png

Categories: Java Projects: Java application

beanshell_netbeans2.png

  • Second, set project name and project location.

beanshell_netbeans3.png

  • Third, set jBEAM.jar as library:

First of all, right click on Libraries. Afterwards, click on the button Create -> new library

beanshell_netbeans5.png

Library name -> OK

beanshell_netbeans4.png

Select Tab “Classpath”
 -> Add JAR/Folder…
 -> Select jBEAM.jar -> OK

beanshell_netbeans6.png

After finishing the import of jBEAM library.

beanshell_netbeans7.pngbeanshell_netbeans8.png

Creating of Script Files in NetBeans

First, create a main class to start script method.

public class Main {
public static void main(String[] args) {
 // Instance of new Scripting class.
 Scripting scripting = new Scripting();
 // Start script which is defined in the scripting class.
 scripting.runScript();
}
}

Second, create a Scripting class: for script files, the scripting() method is important. The scripting method can be copied and inserted in a BSH File which is edited by Note Pad.

import com.AMS.jBEAM.*;
import org.asam.cea.Cea2Bus.*;

class Scripting {
// Declaration of Variables. These Variables also exist in jBEAM, therefore, they need not to be typed in a script file.
// If the package com.AMS.jBEAM.* is imported in the top of class, the sequence com.AMS.jBEAM can be ignored.
 jBEAM jB;
  ScriptService jS;
  ComponentManager jC;
  DesktopManager jG;
  DataItemManager jD;
  org.asam.cea.Cea2Bus.Cea2BusIF cea2Bus;

 // Constructor of scripting class
 Scripting() {
  // Initialize the internal variables of the scripting service.
         jB = newjBEAM();
          jS = (ScriptService) jB.getScriptService();
          jC = (ComponentManager) jB.getComponentManager();
          jG = (DesktopManager) jB.getDesktopManager();
          jD = (DataItemManager) jB.getDataItemManager();
          cea2Bus = (Cea2BusIF) jB.getDataItemManager();
 }
 // init of new jBEAM – method newjBEAM returns new jBEAM
 private jBEAM newjBEAM() {
  if (jB != null) {
   return jB;
  }
       // Make an instance of jBEAM
  try {
    jB = new jBEAM(); // it's a 'Printable' JFrame
  } catch (Exception exc) {
    exc.printStackTrace();
  }
  /*
   * Parameters for the hash table with presetting: String "Locale" Locale
   * "MenuBar" Boolean "LoggerStartCondition" Boolean "PreferredDataArraySize" Integer
   * "WindowTitle" String "WindowRectangle" Rectangle
   */

   HashMap<String, Object> preSettings = new HashMap<String, Object>();
   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  int x = 5;
  int y = 5;
  int requestedWidth = 500;
  int requestedHeight = 400;
   preSettings.put("WindowRectangle", new Rectangle(x, y, requestedWidth, requestedHeight));
   preSettings.put("MenuBar", new Boolean(true));
   preSettings.put("LoggerStartCondition", "Warning");
   preSettings.put("WINDOW_TITLE", "jBEAM for Scripting");
  try {
    jB.init(this, preSettings); // start jBEAM
  } catch (Exception exc) {
    exc.printStackTrace();
        } catch (Error err) {
             err.printStackTrace();
       return null;
        }
        return jB;
 }
 void runScript (){
  // Special script method -> can be copied in a bsh file
  // First, delete all existing components.
     jB.deleteAllObjects();
 }
}

Start script in NetBeans

Click on the beanshell_netbeans9.png button to start the program in NetBeans.

Click on thebeanshell_netbeans10.png button to debug the script.

To toggle breakpoints, click in the gray frame in front of the starting line.

beanshell_netbeans11.png

External links

Tags:

Copyright by Kistler Chemnitz GmbH 2021
XWiki Enterprise 4.4 - Documentation