B4J Question [RESOLVED] Do Combo Box' support text and a return value other then the text ?

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hi

In vb6 a combo box item has 2 properties, Text and item data, is this possible in b4j ?

Regards

John.
 

MarkusR

Well-Known Member
Licensed User
Longtime User
u can store a type or class in the .tag property.
i would use a label to show the selection text and a button to open a selection list window.
after selection from list only type data(id as int,text as string) is stored in the label.tag and u set the label.text = data.text.

u can also create a custom view (user control) for it.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
No. But you can create a global Map. Use the String Values als Key and put any data in the Map then using this KEy.
You easily can restore the Data from the Map later when the user select one item...
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
A Combobox / ChoiceBox can contain any object, you just need to handle the string converter to actually show the item how you want it in the control.

for example suppose you had declared a type
B4X:
type myItem(name as string,code as int)

This can be added to a Combobox but you would see the items listed as [IsInitialized=false, text=one, code=1]

By defining the string converter you can have it just display the 'name' part in the control, but still have access to the other data.

Creating the string converter requires a bit of pure java but I can give you an example of it working if you want.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
The example code as it can be useful to others too (Uses JavaObject library)
B4X:
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

*** to see the difference comment out the line asJO(cb)….. this removes the string converter

changed java code in fromString to better logic
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
in the example code the sub
B4X:
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
can become
B4X:
Sub cb_SelectedIndexChanged(Index As Int, Value As item)
 Log(Value.code)
End Sub
which saves creating an extra item just to get the values
 
Upvote 0

vmag

Active Member
The example code as it can be useful to others too (Uses JavaObject library)
.............

Hello! Can you tell me how to do the same with ListView ?
I tried just replacing ComboBox with Listview, but it didn't work
error in the Return o line in the procedure
B4X:
Sub asJO(o As JavaObject)As JavaObject
Return o
End Sub
ERROR: java.lang.RuntimeException: Method: setConverter not found in: javafx.scene.control.ListView
........

And by the way, if you comment the string as suggested above,
B4X:
asJO(cb).RunMethod("setConverter",Array(tc))
the ListView works, there is no error, but it does not display the data correctly (without conversion)
 
Last edited:
Upvote 0
Top