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"):
using {
"javax.swing"
"java.awt"
"java.awt.event"
}The samples are in samples/UsingJava/gui/.
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:


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:
- every call of Ring to Java runs on the event thread; the Ring thread waits for the answer and runs the callbacks meanwhile;
- an event (a listener that is a Ring function:
"onClick") is queued for the Ring thread, which runs it injMainLoop(),jSleep(),jWaitUntil(),jPump()or while it waits for a call; - a listener without an answer (
actionPerformed,mouseClicked...) does not hold the event thread: the Ring function can call anything, also modal dialogs (jMessage,JFileChooser.showOpenDialog,dispose()), and a Swing timer keeps running while the dialog is open; - a listener with an answer (a comparator, a cell renderer) holds the event thread until the Ring function ends; the calls inside it run at once on the Ring thread.
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#
| Function | Meaning |
|---|---|
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).
func onKey aArgs
cKey = aArgs[1].getActionCommand()Rules that matter for windows#
new JFrametakes the title:new JFrame("Title"). A constructor with more values is@New:(new GridLayout).@New([4,4,4,4]),(new Font).@New(["Monospaced",1,14]).- Methods that have a version with one value and a version with two (
setSize(Dimension)/setSize(w,h),add(Component)/add(Component,constraint)) take one parameter:oFrame.setSize([320,160]),oFrame.add([oPanel,"North"]). A name that has a version without values and versions with values is two methods:oField.getText()andoField.@getText([0,5]). NULL(an empty text) is the null reference for a parameter that is an object:oFrame.setLocationRelativeTo(NULL).- Numbers that Java gives back as
intare Ring numbers; do not add a number to a text ("x" + nCountis fine,nCount + "x"is an error in Ring): usestring(n) + " items". - Custom painting needs a Java class that overrides
paintComponent, which Ring cannot write. Draw into aBufferedImagewithGraphics2Dand show it in aJLabel(see06_drawing.ring).
Samples#
| Sample | Shows |
|---|---|
01_hello_window.ring | frame, label, button, a listener |
02_form.ring | grid layout, text fields, combo box, check box, message boxes |
03_todo_list.ring | JList with a model, Enter in a field, JScrollPane |
04_table.ring | JTable with a DefaultTableModel from Ring data, sorting, selection events |
05_clock.ring | a timer that updates a label |
06_drawing.ring | Java2D into an image |
07_dialogs.ring | message, question and input dialogs |
08_calculator.ring | one listener for 17 buttons |
09_editor.ring | menu bar, text area, file chooser |
10_tree.ring | JTree with DefaultMutableTreeNode |
.\run_all.ps1 runs them all: each window opens and closes after 2.5 seconds.