Window builders
A form with a menu in a few lines: jWindow, jForm, jBorder, jRow and jMenuBar.
What you will learn#
jWindow,jShowandjMainLoop,- controls with a handler:
jField,jCombo,jCheck,jButton, - layouts:
jForm,jColumn,jRow,jBorder,jPad, - a menu bar with a shortcut.
The program#
# Tutorial 23: window builders - a form in a few lines
load "java.ring"
oWin = jWindow("Greeting",380,200)
oName = jField("",16,"onGreet")
oLanguage = jCombo(["English","Arabic","French"],"onGreet")
oLoud = jCheck("Loud",false,"onGreet")
oOutput = jLabel(" ")
oForm = jForm([
["Name",oName],
["Language",oLanguage],
["",oLoud]
],8)
oWin.add([jBorder(
jPad(jColumn([oForm,oOutput],12),12),
NULL,
jPad(jRow([jButton("Greet","onGreet"),jButton("Close","onClose")],8),8),
NULL,
NULL
),"Center"])
jMenuBar(oWin,[
["File",[
["Greet","onGreet","ctrl G"],
"-",
["Close","onClose"]
]]
])
jShow(oWin)
jAutoClose()
jMainLoop()
func onGreet aArgs
aWords = ["Hello","Marhaba","Bonjour"]
cWord = aWords[oLanguage.getSelectedIndex() + 1]
cText = cWord + " " + oName.getText()
if oLoud.isSelected()
cText = upper(cText) + "!"
ok
oOutput.setText(cText)
func onClose aArgs
jStopLoop()
How it works#
The builders return the objects of Swing, so oName.getText() and every other method still work. A handler is the name of a Ring function; here one function serves the field, the combo box, the check box, the button and the menu item. The program needs no using { }: the builders load Swing and switch the program to GUI mode. See Window builders for the list.
Exercises#
- Add a slider for the size of the greeting.
- Put the controls in a
jGridinstead of ajForm. - Add a menu item that clears the name.