Java Array

Hello all :-)

How can I work with a Java Array from Servoy (JavaScript).

I’ve tried with

var widths = java.lang.reflect.Array.newInstance(java.lang.Number, 5);

but I don’t knnow how to populate widths variable

One more thing. In Servoy 5.x the sentence java … had autocompletion, in Servoy 6.x has not, and the following warning appears

The function newInstance(java.lang.Class,Number) is not applicable for the argumetns (Class<java.lang.Number>,Number)

Best regards. Roberto Blasco.

Why do you want to create a Java array in the first place? What type of Array should it be?

Try this:

	var widths = java.lang.reflect.Array.newInstance(java.lang.Class.forName("java.lang.Number"), 5);
	widths[0] = 3;
	widths[1] = 43;
	widths[2] = 21;
	widths[3] = 33;
	widths[4] = 55;
	widths[6] = 44;

Then examine the contents of each width and widths.length in the debugger!

Hi Patrick.

I want to reuse the methods from a Java Object I get from a plugin I’m developing. One of this methods needs a Java Array to be initialized.

That’s why I need a Java array :D

Hi tparry

I’ve tried your code, but Servoy throws this exception

The choice of Java constructor setWidths matching JavaScript argument types ([Ljava.lang.Number;) is ambiguous; candidate constructors are:
void setWidths(float)
void setWidths(int) (C:\Users\roberto\Programas\servoy.6\servoy.workspace.pericias.central.v2\rj_pericias_central_v2\forms\menu_casos.js#65)
The choice of Java constructor setWidths matching JavaScript argument types ([Ljava.lang.Number;) is ambiguous; candidate constructors are:
void setWidths(float)
void setWidths(int) (C:\Users\roberto\Programas\servoy.6\servoy.workspace.pericias.central.v2\rj_pericias_central_v2\forms\menu_casos.js#65)
at C:\Users\roberto\Programas\servoy.6\servoy.workspace.pericias.central.v2\rj_pericias_central_v2\forms\menu_casos.js:65 (onAction)

This is the code I’m trying

	var jdoc = plugins.iTextRTF.JDoc();		// Initialize plugin
	jdoc.initDoc();							// Initialize doc
	var doc = jdoc.getDoc();				// Get Java Document Object
	doc.open();								// Open doc to be written
	var table = itext.Table(3);				// Get Java Table Object
	// Java Array to set the Table widths
	var widths = new java.lang.reflect.Array.newInstance(java.lang.Number, 5);
	widths[0] = 1;
	widths[1] = 2;
	widths[2] = 2;
	table.setWidths(widths);				// Table widths

Whre doc and table are Java Objects.

Thanks a lot for the replies :-)

I’d rather change the setter for widths. Change it to take an Object and then have your JS method just pass a JS array. Many plugins work with arrays and they do not need to be native Java arrays.

or, if you already know you expect Numbers simply do in your plugin

public void js_setWidths(Number widths)

then you can do

var vArray = [1, 2, 3, 4, 5]
table.setWidths(vArray)

Thanks patrick

I’m gonna try it