RingJavafor Ring

Windows, events and dialogs (Swing and AWT)

Swing is in java.desktop, so all its classes are generated: name its packages in using { } and make windows with new JFrame("Title").

Swing is in java.desktop, so all its classes are generated. Name its packages and make a window with new JFrame("Title"):

Ring
using {
	"javax.swing"
	"java.awt"
	"java.awt.event"
}

The samples are in samples/UsingJava/gui/.

Ring
load "java.ring"
using { # also calls jGui(): the calls of Ring run on the event thread of Swing
	"javax.swing"
}

oFrame = new JFrame("Hello")
oFrame.setDefaultCloseOperation(2)             # DISPOSE_ON_CLOSE
oFrame.setSize([320,160])
oButton = new JButton("Click me")
oButton.addActionListener("onClick")
oFrame.add([oButton,"Center"])
oFrame.setVisible(true)

jMainLoop()                                    # runs the events until the last window is closed

func onClick aArgs
	jMessage("Clicked","Hello")

The window of this program, as ringdoc shoot photographs it from the real sample, and a form after the program filled it:

The window of the first sample
01_hello_window.ring
A form with its fields filled
02_form.ring after the fields were filled

All ten windows are on the page Swing samples.

The event thread#

Swing has one thread for everything that touches a window: the event thread (EDT). A Ring program has its own thread. jGui() joins them. using { "javax.swing" } and using { "java.awt" } call it for you (as using does in RingDotNet), so a program needs no extra line:

jGuiMode(false) after the using puts the usual mode back. In the usual mode windows still work, but the events are served only when the program calls jPump() / jSleep() and a modal dialog blocks the program.

Functions#

FunctionMeaning
jGui()GUI mode, with the system look and feel; using { "javax.swing" } or { "java.awt" } calls it (JavaFX has its own mode)
jMainLoop()run the events until no window is showing, or until jStopLoop()
jStopLoop()end jMainLoop() (call it from an event function)
jInvokeLater(cFunction)run a Ring function later on the event thread (SwingUtilities.invokeLater)
jTimer(nMillis,cFunction)a Swing Timer that calls a Ring function: oTimer.start(), oTimer.stop(), oTimer.setRepeats(0)
jMessage(cText,cTitle)a message box
jConfirm(cText,cTitle)Yes / No: true for Yes
jInput(cText,cTitle)a box for a text: the text, or "" when cancelled
jColor(r,g,b)a java.awt.Color
jScreenSize()[width, height] of the screen
jAutoClose()closes the windows after RINGJAVA_AUTOCLOSE milliseconds when that environment variable is set (for the automatic run of the samples); with --selftest it first runs the selfTest() function of the program (see the applications)

Events#

The event function gets one list: aArgs[1] is the event object (ActionEvent, MouseEvent, ListSelectionEvent ...), and the global cJavaMethod is the name of the listener method (a listener with several methods, such as MouseListener or DocumentListener, calls the same function for all of them: look at cJavaMethod).

Ring
func onKey aArgs
	cKey = aArgs[1].getActionCommand()

Rules that matter for windows#

Samples#

SampleShows
01_hello_window.ringframe, label, button, a listener
02_form.ringgrid layout, text fields, combo box, check box, message boxes
03_todo_list.ringJList with a model, Enter in a field, JScrollPane
04_table.ringJTable with a DefaultTableModel from Ring data, sorting, selection events
05_clock.ringa timer that updates a label
06_drawing.ringJava2D into an image
07_dialogs.ringmessage, question and input dialogs
08_calculator.ringone listener for 17 buttons
09_editor.ringmenu bar, text area, file chooser
10_tree.ringJTree with DefaultMutableTreeNode

.\run_all.ps1 runs them all: each window opens and closes after 2.5 seconds.

RingJava - use Java classes and methods from Ring programs.