Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

lib-swing

lib-swing is a RichTea library for building desktop GUI applications using Java Swing. It provides declarative RichTea functions for every major Swing component, with event handling wired directly to RichTea branches.


Components

Container components

Function Swing class Implicit attr/branch Description
Frame JFrame title / :content Top-level application window.
Panel JPanel — / :content General-purpose container.
SplitPane JSplitPane — / :content Two-pane split container. Requires exactly two children.
ScrollPane JScrollPane — / :content Scrollable wrapper around a single child.
TabbedPane JTabbedPane — / :content Tabbed container; child component names become tab titles.

Control components

Function Swing class Implicit attr Description
Label JLabel text Static text label.
Button JButton text Clickable button.
RadioButton JRadioButton text Radio button; use buttonGroup to group.
ComboBox JComboBox data Drop-down list populated from a RichTea array.
Slider JSlider Slider control.
TextField JTextField text Single-line text input.
TextArea JTextArea text Multi-line text input.

Menu components

Function Swing class Implicit attr Description
MenuBar JMenuBar — / :content Top-level menu bar.
Menu JMenu text / :content Drop-down menu inside a MenuBar.
MenuItem JMenuItem text Clickable item inside a Menu.
MenuItemRadioButton JRadioButtonMenuItem text Radio-button menu item.

Layout managers

Function Java class Description
FlowLayout java.awt.FlowLayout Flows components left-to-right.
BorderLayout java.awt.BorderLayout North/South/East/West/Center regions.
GridLayout java.awt.GridLayout Fixed-size grid of cells.

Common Attributes (all components)

All components inherit from CreateAWTComponent and support:

Attribute Description
x X position in the parent container (default: 0).
y Y position in the parent container (default: 0).
width Component width in pixels (default: preferred width).
height Component height in pixels (default: preferred height).
name Component name, used as the tab title in TabbedPane.

Events

All components automatically wire up RichTea branches for every supported Swing event. To respond to an event, add a branch whose name matches the event.

Supported events (all components)

Branch name Fired when
:actionPerformed Button clicked, menu item selected, combo selection changed.
:mouseClicked Mouse button clicked.
:mousePressed Mouse button pressed.
:mouseReleased Mouse button released.
:mouseEntered Cursor entered the component.
:mouseExited Cursor left the component.
:keyPressed Key pressed while component has focus.
:keyReleased Key released while component has focus.
:keyTyped Key typed (pressed and released).
:focusGained Component gained keyboard focus.
:focusLost Component lost keyboard focus.
:itemStateChanged Selection state changed (checkboxes, combo boxes).
:componentResized Component was resized.
:componentMoved Component was moved.
:propertyChange A bound property changed.

Window-specific events (Frame only)

Branch name Fired when
:windowOpened Window first made visible.
:windowClosing User initiates closing (e.g. clicks ✕).
:windowClosed Window has been closed.
:windowIconified Window minimised.
:windowDeiconified Window restored from minimised.
:windowActivated Window brought to foreground.
:windowDeactivated Window sent to background.

Examples

Hello World window

Import("*" from:"modules/std")
Import("*" from:"modules/swing")

Frame("Hello RichTea" width:400 height:200 :{
  Label("Hello, World!" x:150 y:80)
} :windowClosing{
  SystemExit(0)
})

Button click counter

Import("*" from:"modules/std")
Import("*" from:"modules/swing")

Let(count:0)
Let(label:@Label("Clicks: 0" x:140 y:60 width:120 height:30))

Frame("Click Counter" width:400 height:200 :{
  label
  Button("Click me!" x:150 y:110 width:100 height:30 :actionPerformed{
    Increment(attribute:count by:1)
    Set(attribute:label.text to:"Clicks: { count }")
  })
} :windowClosing{
  SystemExit(0)
})

Combo box with selection handler

Import("*" from:"modules/std")
Import("*" from:"modules/swing")

Let(colours:["Red", "Green", "Blue", "Yellow"])

Frame("Colour Picker" width:300 height:150 :{
  ComboBox(data:colours x:80 y:50 width:140 height:30 :actionPerformed{
    Print("Selected: { _.getSelectedItem() }")
  })
} :windowClosing{
  SystemExit(0)
})

Split pane layout

Import("*" from:"modules/std")
Import("*" from:"modules/swing")

Frame("Split View" width:600 height:400 :{
  SplitPane(x:0 y:0 width:600 height:400 :{
    Panel(width:300 height:400 :{
      Label("Left Panel" x:100 y:180)
    })
    Panel(width:300 height:400 :{
      Label("Right Panel" x:100 y:180)
    })
  })
} :windowClosing{
  SystemExit(0)
})

Menu bar

Import("*" from:"modules/std")
Import("*" from:"modules/swing")

Frame("Menu Example" width:500 height:350 :{
  MenuBar(:{
    Menu("File" :{
      MenuItem("Open" :actionPerformed{
        Print("Open clicked")
      })
      MenuItem("Save" :actionPerformed{
        Print("Save clicked")
      })
      MenuItem("Exit" :actionPerformed{
        SystemExit(0)
      })
    })
    Menu("Help" :{
      MenuItem("About" :actionPerformed{
        Print("RichTea lib-swing")
      })
    })
  })
} :windowClosing{
  SystemExit(0)
})

Tabbed pane

Import("*" from:"modules/std")
Import("*" from:"modules/swing")

Frame("Tabs" width:500 height:300 :{
  TabbedPane(x:0 y:0 width:500 height:300 :{
    Panel(name:"Overview" width:500 height:270 :{
      Label("This is the overview tab." x:20 y:20)
    })
    Panel(name:"Settings" width:500 height:270 :{
      Label("This is the settings tab." x:20 y:20)
    })
  })
} :windowClosing{
  SystemExit(0)
})

Key listener

Import("*" from:"modules/std")
Import("*" from:"modules/swing")

Frame("Key Logger" width:400 height:200 :{
  TextField("Type here..." x:100 y:80 width:200 height:30
    :keyTyped{
      Print("Key typed: { _.getKeyChar() }")
    }
  )
} :windowClosing{
  SystemExit(0)
})

Quick Reference

Frame(title width height :content{ ... } :windowClosing{ ... })
Panel(x y width height :content{ ... })
SplitPane(x y width height :content{ childA childB })
ScrollPane(x y width height :content{ child })
TabbedPane(x y width height :content{ Panel(name:"Tab1" ...) ... })
Label(text x y width height)
Button(text x y width height :actionPerformed{ ... })
RadioButton(text x y buttonGroup:"groupName" :actionPerformed{ ... })
ComboBox(data:list x y width height :actionPerformed{ ... })
TextField(text x y width height :keyTyped{ ... })
TextArea(text x y width height)
Slider(x y width height)
MenuBar(:content{ Menu("Name" :content{ MenuItem("Item" :actionPerformed{ ... }) }) })

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages