Fork us on GitHub

Sockets & MultiLine Trees

Steve recently added an implementation of a sockets library w...

Sockets & MultiLine Trees

Steve recently added an implementation of a sockets library which is pretty cool and very timely since we are just about to release our own socket library and unlike his work which supports all Codename One targets we currently only support iOS & Android.

At the moment we only support TCP sockets, we support server socket (listen/accept) on Android but not on iOS. You can check if Sockets are supported using the Socket.isSupported() and whether server sockets are supported using Socket.

To use sockets you can use the Socket.connect(String host, int port, SocketConnection eventCallback) method. To listen on sockets you can use the Socket.listen(int port, Class scClass) method which will instantiate a SocketConnection instance (scClass is expected to be a subclass of SocketConnection) for every incoming connection.

This simple example allows you to create a server and a client assuming the device supports both:

Also in the coming update to Codename One we will include support for multiline entries in trees and more arbitrary tree node types. To support that we added a new flag to indicate if the tree is multiline in which case each node would be a SpanButton instead of a button. This is pretty simple for the basic use case just invoke setMultilineMode(true).

Unfortunately for the complex use cases up until now when we customized a tree entry one had to override the method: protected Button createNodeComponent(Object node, int depth), which relies on the Tree's usage of buttons. So that method is now deprecated (although it will still work for now).
You should now override: protected Component createNode(Object node, int depth), which allows you to return any component type. However, the tree needs methods to receive a click event from the node and to set an icon both of which aren't available in Component. So for that purpose you might need to override: protected void bindNodeListener(ActionListener l, Component node), and: protected void setNodeIcon(Image icon, Component node). Just override them to support your custom component type unless its a Button/SpanButton (or subclass) both of which are supported.

Notice: This post was automatically converted using a script from an older blogging system. Some elements might not have come out as intended.... If that is the case please let us know via the comments section below.

Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.