JLIB -- a high-level interface to the Java AWT
11/28/2001
The key idea is to use the flexibility and expressiveness of
Scheme to create a GUI-building library which allows complex
GUIs to be built by evaluating a simple and declarative expression
whose structure closely resembles the structure of the GUI itself.
The JLIB model is based on five fundamental concepts:
- COMPONENTS -- there are a small number of ways to construct basic components (buttons, windows, ...)
- LAYOUTS -- there are a small number of ways to layout basic components (row, col, table, grid, ...)
- ACTIONS -- there is a simple mechanisms for associating an action to a component
- PROPERTIES -- there are easy ways for setting the font and color of components
- TAGGING -- there is a simple mechanism for giving names to components "in place"
Another key idea is that operations on all components should be as
uniform as possible. For example, there are procedures "readstring" and
"writestring" which allow one to read a "string" from a component, and
write a string onto a component. Thus "writestring" can change the
string on a label, a button, a textfield, a textarea. It can also change the
title of a window or add an item to a choice component. Likewise, readstring
returns the label of a button, the text in a textarea or textfield, the
text of the currently selected item in a choice, the title of a window,
and the text of a label.
Similarly, JLIB is designed so that the component and layout constructors
require a few initial arguments of specified types, followed
by many optional arguments which may apper
in any order and whose types are used to determine their effect.
For example, a font object will change the
font of the component, a color object will change the background color,
a Listener object will add an action listener to the component, a string
will generally become the label of the object.
The current JLIB primitives are listed below:
Components
Components are created using the functions below. The optional arguments are described in the
next section and can be used to set the color, font, and other properties of the component.
The type of the argument is used to determine how it affect the component.
- (window TITLE arg1 arg2 ...)
- (button STRING arg1 arg2 ...)
- (textfield STRING NUMCOLS arg1 arg2 ...)
- (textarea NUMROWS NUMCOLS arg1 arg2 ...)
- (choice arg1 arg2 ...)
- (label STRING arg1 arg2 ...)
- (canvas W H ...)
- (menubar NAME arg1 arg2 ...)
- (menu NAME arg1 arg2 ...)
- (menuitem NAME arg1 arg2 ...)
Layouts
- Tabular layouts
- (row arg1 arg2 ...)
- (col arg1 arg2 ...)
- (splitrow s1 s2 R1 R2 arg1 ...) creates a box containing two rows whose relative sizes are given by the proportion s1::s2
- (splitcol s1 s2 C1 C2 arg2 ...) creates a box containing two cols whose relative sizes are given by the proportion s1::s2
- (table ROWS COLS arg1 arg2 ...) creates a box consisting of the specified numbers of rows and columns
- (grid NUMROWS NUMCOLS arg1 arg2 ...) creates a table where all cells have the same size
These layouts all allow some additional arguments which specify how the components are placed in a cell and how they
expand when the cell is enlarged:
- placement: 'center 'north 'northeast 'east 'southeast ...
- expansion: 'horizontal 'vertical 'both 'none
- Border Layout
The args can specify properties of the container (e.g. color, font, etc.)
and they can also include the following terms which add up to five components to
specified parts of the layout. When the layout is resized,
the center component expands in both ways, the north and south components expand
horizontally only, and the east and west expand vertically only. The following
positional args must precede the other args (font/color/etc.)
- (north COMPONENT)
- (south COMPONENT)
- (east COMPONENT)
- (west COMPONENT)
- (center COMPONENT)
- Card Layout
- (cards (card NAME1 COMPONENT) (card NAME2 COMPONENT) ....) -- this creates a COMPONENT
with the CardLayout consisting of several NAMEd components. Only one of the cards is visible at
a time
- (showcards CARDS NAME) -- this causes the CARDS layout to make the NAMEd card visible.
Arguments to Components and Layouts
- Colors -- these set the background color of the component
- standard colors: black, white, red, green, ...
- (color R G B)
- (color 0xRRGGBB)
- Fonts -- these set the font of the component
- (Courier N) (CourierBold N) (CourierItalic N)
- (TimesRoman N) (TimesRomanBold N) (TimesRomanItalic N)
- (Helvetica N) (HelveticaBold N) (HelveticaItalic N)
- (java.awt.Font. NAME STYLE SIZE)
- Actions -- these specify the actions to be carried out when the component receives an action event
- (action (lambda (e) COMMANDS))
- Size -- (size WIDTH HEIGHT)
- window -- set the size of the window
- textarea -- set the size of the textarea in rows and columns
- Strings
- textarea -- store string in the textarea
- choice -- add the string as one of the choices
- menu -- add the string as a simple menuitem
- menuitem -- set the label of the menuitem to the string
- Components
- for layouts, add the component to the layout
- Tags
- (let ((tag (maketagger))) EXPR) creates a tagger "tag" whose scope is the EXPR
- (tag STRING) -- returns the component which has been tagged with the
string
- (tag STRING COMPONENT) -- tags the component with the string and
returns the COMPONENT
- lambdas
- (lambda(C) COMMANDS) -- commands to be carried out on the component "C" when it is being created,
for example
- (lambda (C) (.setForeground C blue)) -- sets the foreground color of the component to blue
- (lambda (C) (.hide C)) -- makes C invisible (although it still takes up space in the layout)
- (lambda (C) (display (list "just created " C)) (newline)) -- prints a message when C is created
Actions
-
Reading/Writing on Components
- (readstring COMPONENT) -- reads the text on a component as a string
- (readexpr COMPONENT) -- reads the text on a component as a Scheme term
- (readexprlist COMPONENT) -- reads the text on a component as a sequence of Scheme terms and returns a list containing those terms
- (writeexpr COMPONENT OBJ) -- convert the object to a string and write on the component
- (writelnexpr COMPONENT OBJ) -- same as above, but add a new line at the end
- (appendexpr COMPONENT OBJ) -- append the stringified object to the text on the component
- (appendlnexpr COMPONENT OBJ) -- same as above, but add a newline at the end
-
Other Common Java Actions
- (.hide COMP) (.show COMP) hiding or showing a component (or window)
- (.pack WIN) (.resize WIN) resizing a window
- (.setForeground COMP COLOR) (.setBackground COMP COLOR)
- (.setFont COMP FONT)
- (.addMouseListener COMP (Listener. (lambda(e) (.getX e) (.getY e) ...)))
- (.addMouseMotionListener COMP (Listener. (lambda(e) (.getX e) (.getY e) ...)))
- (.fillRect (.getGraphics g) x y w h)
- (.fillRect (.bufferg$ CANVAS) x y w h) (.repaint CANVAS) -- canvas objects have an
associated backbuffer which you can access using (.bufferg$ CANVAS).
The current JLIB is accessible here
and we have a SWING version as well, which is accessible here
To get the most uptodate version you should download the latest jscheme source from
CVS.