B4J Question Create a gui in text mode

Status
Not open for further replies.

Alessandra Pellegri

Active Member
Licensed User
Longtime User
I'd like to create a program in text mode with a gui similar old style dos installation programs, or actually raspi-config under linux.

Is there any starting point ?

Actually I don't need of any complex interface. Just some text and some progress bar done with # chars.

How can I put some text in a precise location on the screen ?

Thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The way to do it is with this project: https://github.com/mabe02/lanterna

A quick test shows that it can work nicely:
B4X:
#AdditionalJar: lanterna-3.0.1
Sub Process_Globals
    Private Terminal, Screen As JavaObject    
    Private TextGraphics As JavaObject
End Sub

Sub AppStart (Args() As String)
    Terminal = CreateTerminal
    Screen = CreateScreen(Terminal)
    TextGraphics = Screen.RunMethod("newTextGraphics", Null)
    Screen.RunMethod("startScreen", Null)
    Screen.RunMethod("clear", Null)
    
    For x = 1 To 10
        SetCharacter("*", x, 20)
    Next
    Refresh
    Dim o As Object = Screen.RunMethod("readInput", Null)
    Log(GetType(o))
    Screen.RunMethod("stopScreen", Null)
End Sub

Sub CreateTerminal As JavaObject
    Dim factory As JavaObject
    Return factory.InitializeNewInstance("com.googlecode.lanterna.terminal.DefaultTerminalFactory", Null).RunMethod("createTerminal", Null)
End Sub

Sub CreateScreen(Term As Object) As JavaObject
    Dim s As JavaObject
    Return s.InitializeNewInstance("com.googlecode.lanterna.screen.TerminalScreen", Array(Term))
End Sub

Sub SetCharacter(Char1 As Char, Column As Int, Row As Int)
    TextGraphics.RunMethod("setCharacter", Array(Column, Row, Char1))
End Sub

Public Sub SetString (s As String, Column As Int, Row As Int)
    TextGraphics.RunMethod("putString", Array(Column, Row, s))
End Sub


Sub Refresh
    Screen.RunMethod("refresh", Null)
End Sub

This is a large library and it will require some work to wrap it (with Java or JavaObject).
 
Upvote 0

a.consorti

Member
Licensed User
Hi Erel,
only you can help me. Based on your example I tried lanterna bu I can't add a Button with relative click event.
The code I use produces no errors but the event does not fire. Probably the error is in the use of Runnable but I don't understand where.
Could you help me understand?
Thank you
Example used:
    '---------------------------------
    ' Setup terminal and screen layers
    '---------------------------------
    Terminal = CreateTerminal
    Screen = CreateScreen(Terminal)
    Screen.RunMethod("startScreen", Null)
    
    Screen.RunMethod("clear", Null)
    TextGraphics = Screen.RunMethod("newTextGraphics", Null)
    '--------------------------------
    ' Create Create panel to hold components
    '--------------------------------
    
    gridlayout = CreateGridLayout(2)
    pannello = CreatePanel
    pannello.RunMethod("setLayoutManager",Array As Object(gridlayout))
    
    Dim lbl1 As JavaObject = CreateLabel("Num.1:")
    Dim tBox1 As JavaObject = CreateTextBox(CreateTerminalSize(20,1))
    Dim lbl2 As JavaObject = CreateLabel("Num.2:")
    Dim tBox2 As JavaObject = CreateTextBox(CreateTerminalSize(20,1))
    Dim spazio_vuoto As JavaObject = CreateEmptySpace(CreateTerminalSize(0,0))


    ' Creazione e gestione del primo pulsante

    Dim runnable1 As JavaObject = CreateRunnable("Button1_Click")
    Dim button1 As JavaObject
    button1.InitializeNewInstance("com.googlecode.lanterna.gui2.Button", Array("TEST", runnable1))



  
    
    pannello.RunMethod("addComponent",Array As Object(lbl1))
    pannello.RunMethod("addComponent",Array As Object(tBox1))
    pannello.RunMethod("addComponent",Array As Object(lbl2))
    pannello.RunMethod("addComponent",Array As Object(tBox2))
    pannello.RunMethod("addComponent",Array As Object(spazio_vuoto))
    pannello.RunMethod("addComponent",Array As Object(button1))
    '--------------------------------
    ' Create window to hold the panel
    '--------------------------------
    form = CreateBasicWindow'
    form.RunMethod("setComponent",Array As Object(pannello))
    'SetFullScreenHint(BasicWindow)
    'BasicWindow.RunMethod("setHints",Array As Object(WindowHint.GetField("FULL_SCREEN")))
    
    
    
    '-------------------------
    ' Create gui and start gui
    '-------------------------
    gui = MultiWindowTextGUI(Screen, CreateWindowManager, CreateEmptySpaceTextColor(CreateTextColor("RED"))) 'Colore di sfondo
    gui.RunMethod("addWindowAndWait", Array As Object(form))
    
    End Sub

Sub Button1_Click
    Log("Button 1 simulated for add 2 numbers")
End Sub

Sub CreateRunnable(MethodName As String) As Object
    Dim jo As JavaObject
    jo.InitializeNewInstance("java.lang.Object", Null)
    Return jo.CreateEvent("java.lang.Runnable", MethodName, False)
    
End Sub

Sub CreateTerminalSize(colonne As Int,righe As Int) As JavaObject
    Dim componente As JavaObject
    Return componente.InitializeNewInstance("com.googlecode.lanterna.TerminalSize",Array As Object(colonne,righe))
End Sub


Sub CreateButton(etichetta As String) As JavaObject
    Dim componente As JavaObject
    Return componente.InitializeNewInstance("com.googlecode.lanterna.gui2.Button",Array As Object(etichetta))
End Sub

Sub CreateButtonRunnable(etichetta As String,evento As String) As JavaObject
    Dim componente As JavaObject
    Return componente.InitializeNewInstance("com.googlecode.lanterna.gui2.Button",Array As Object(etichetta,evento))
End Sub

Sub CreateLabel(etichetta As String) As JavaObject
    Dim componente As JavaObject
    Return componente.InitializeNewInstance("com.googlecode.lanterna.gui2.Label",Array As Object(etichetta))
End Sub

Sub CreateTextBox(par_terminal_size As JavaObject) As JavaObject
    Dim componente As JavaObject
    Return componente.InitializeNewInstance("com.googlecode.lanterna.gui2.TextBox",Array As Object(par_terminal_size))
End Sub


Sub CreateGridLayout(colonne As Int) As JavaObject
    Dim gl As JavaObject
    Return gl.InitializeNewInstance("com.googlecode.lanterna.gui2.GridLayout",Array As Object(colonne))
End Sub

Sub CreatePanel As JavaObject
    Dim pnl As JavaObject
    Return pnl.InitializeNewInstance("com.googlecode.lanterna.gui2.Panel", Null)
End Sub

Sub CreateBasicWindow As JavaObject
    Dim window As JavaObject
    Return window.InitializeNewInstance("com.googlecode.lanterna.gui2.BasicWindow", Null)
End Sub

Sub CreateEmptySpace(par_terminal_size As JavaObject) As JavaObject
    Dim componente As JavaObject
    Return componente.InitializeNewInstance("com.googlecode.lanterna.gui2.EmptySpace",Array As Object(par_terminal_size))
End Sub

Sub CreateEmptySpaceTextColor(par_text_color As JavaObject) As JavaObject
    Dim componente As JavaObject
    Return componente.InitializeNewInstance("com.googlecode.lanterna.gui2.EmptySpace",Array As Object(par_text_color))
End Sub


Sub CreateTextColor(color As String) As JavaObject
    Dim textColorFactory As JavaObject
    textColorFactory.InitializeStatic("com.googlecode.lanterna.TextColor$Factory")

    ' Use the factory to create a TextColor
    Dim tc As JavaObject
    tc = textColorFactory.RunMethod("fromString", Array As Object(color))
    Return tc
End Sub

Sub WindowHint As JavaObject
    Dim wh As JavaObject
    Return wh.InitializeStatic("com.googlecode.lanterna.gui2.Window.Hint")
End Sub

Sub MultiWindowTextGUI(scr As Object,windowmanager As Object,bg As Object) As JavaObject
    Dim gui As JavaObject
    Return gui.InitializeNewInstance("com.googlecode.lanterna.gui2.MultiWindowTextGUI", Array As Object(scr,windowmanager,bg))   
End Sub


Sub CreateWindowManager As JavaObject
    Dim wm As JavaObject
    Return wm.InitializeNewInstance("com.googlecode.lanterna.gui2.DefaultWindowManager", Null)
End Sub

Sub CreateTerminal As JavaObject
    Dim factory As JavaObject
    Return factory.InitializeNewInstance("com.googlecode.lanterna.terminal.DefaultTerminalFactory", Null).RunMethod("createTerminal", Null)
End Sub

Sub CreateScreen(Term As Object) As JavaObject
    Dim s As JavaObject
    Return s.InitializeNewInstance("com.googlecode.lanterna.screen.TerminalScreen", Array(Term))
End Sub

Sub SetCharacter(Char1 As Char, Column As Int, Row As Int)
    TextGraphics.RunMethod("setCharacter", Array(Column, Row, Char1))
End Sub

Public Sub SetString (s As String, Column As Int, Row As Int)
    TextGraphics.RunMethod("putString", Array(Column, Row, s))
End Sub


Sub Refresh
    Screen.RunMethod("refresh", Null)
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The Button1_Click sub is not correct. The JavaObject CreateEvent requires a different signature. This works for me:

B4X:
Sub Button1_Click_Event(EventName As String, Args() As Object)
    Log("Button 1 simulated for add 2 numbers")
End Sub

If you are running this as a Non_UI app, you also need to add StartMessageLoop to the end of the AppStart sub.

I hadn't come across this library before, it could be quite interesting.
 
Last edited:
Upvote 0
Status
Not open for further replies.
Top