Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Dim cb As ComboBox
Type item (text As String,code As Int)
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show
cb.Initialize("cb")
Dim tc As JavaObject
' the fully qualified name of the class defined in the java code below
tc.InitializeNewInstance("b4j.example.main.typeConverter",Null)
' set the stringconverter for the combobox
asJO(cb).RunMethod("setConverter",Array(tc))
' add some items that are ojects
cb.Items.Add(makeItem("one",1))
cb.Items.Add(makeItem("two",2))
cb.Items.Add(makeItem("three",3))
MainForm.RootPane.AddNode(cb,10,10,200,20)
End Sub
Sub cb_SelectedIndexChanged(Index As Int, Value As Object)
Dim titem As item = Value
' retrieve the code part of the object
Log(titem.code)
End Sub
' make the items
Sub makeItem(s As String,i As Int) As item
Dim ni As item
ni.text = s
ni.code = i
Return ni
End Sub
' return a javaobject reference
Sub asJO(o As JavaObject)As JavaObject
Return o
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
#if java
import javafx.util.StringConverter;
// _item is the name of the class that Type item(...) creates in B4J
public static class typeConverter extends StringConverter<_item>
{
// Method to convert a _item Object to a String
@Override
public String toString(_item o)
{
return o == null? null : o.text;
}
// Method to convert a String to an _item Object
// code below is only needed if combo is editable by the user
@Override
public _item fromString(String string)
{
_item o = new _item();
if (string == null)
{
return o;
}
Object[] ob = string.split(",");
o.text = (String)ob[0];
o.code = (Integer) Integer.parseInt((String)ob[1]);
return o;
}
}
#End If