RingJavafor Ring

Window builders

A form with a menu in a few lines: jWindow, jForm, jBorder, jRow and jMenuBar.

What you will learn#

The program#

samples/UsingJava/tutorials/ch23_builders.ring
# 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()
The greeting form
ch23_builders.ring

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#

  1. Add a slider for the size of the greeting.
  2. Put the controls in a jGrid instead of a jForm.
  3. Add a menu item that clears the name.

Next#

A database with JavaDatabase

RingJava - use Java classes and methods from Ring programs.