Skip to content
This repository

Hafni is a java swing wrapper for the clojure language focusing on how components change.

tree: 0eb4bed309

Fetching latest commit…

Cannot retrieve the latest commit at this time


Hafni is a java swing wrapper for the clojure language focusing on how components change. This does not only mean that the content displayed should be easy to change, but also that events coming from that content should be handled in a convenient way.

This is a small taste of Hafni, for a more thorough explanation of the parts of Hafni, see Usage below. The following example comes from Hafni.test.swing.action/button-test2

 (frame :content (comp-and-events (button :text "*")
                                  :act (flow (output-arr this :text) >>>
                                             (arr #(str % "*")) >>> (input-arr this :text)))
        :size 200 200 :dont_exit_on_close)

This creates a frame (a window if you so will), with a button as content, size 200x200 and which doesn't close when the frame is closed (useful when working in the repl).

 (comp-and-events (button :text "*")
                  :act (flow (output-arr this :text) >>>
                             (arr #(str % "*")) >>> (input-arr this :text)))

We first create a button with the text "*".

 (button :text "*")

Then, when the button is pressed, the buttons text is extracted, another "*" is added to the end of that text, and set as the current text of the button.

 (flow (output-arr this :text) >>>
       (arr #(str % "*")) >>> (input-arr this :text)))

What happens is that (output-arr this :name) receives the current text, which is then sent to the function #(str % "*") (the arr can be ignored for now). The result of that function is then sent to the buttons text with (input-arr this :text). The result is that the text of the button changes in the following way:


To accomplish this, Hafni uses the datatypes Event and Arrow which are both explained in the wiki.

See Wiki/usage

Add [hafni "1.0.4-SNAPSHOT"] as a dependency in leiningen.

To the left is the java class or function that is wrapped, to the right is the corresponding function used in Hafni.

 AbstractAction         action
 ButtonGroup            button-group
 JButton                button
 JCheckBox              check-box
 JCheckBoxMenuItem      check-box-menu-item
 JMenuItem              menu-item
 JRadioButton           radio-button
 JRadioButtonMenuItem   radio-button-menu-item
 JToggleButton          toggle-button

 JComboBox              combo-box
 JList                  jlist
 JSpinner               spinner
 JTabbedPane            tabbed-pane


 BorderLayout           border-layout
 BoxLayout              box-layout
 CardLayout             card-layout
 FlowLayout             flow-layout
 GridLayout             grid-layout

 JMenu                  menu
 JMenuBar               menu-bar
 JToolBar               tool-bar

 JTable                 table

 JPasswordField         password-field
 JTextArea              text-area
 JTextField             text-field
 JTextPane              text-pane

 JTree                  tree

 File                   file
 Font                   font
 Color                  color

 JFrame                 frame
 JIcon                  icon
 JLabel                 label
 JProgressBar           progress-bar
 JScrollPane            scroll-pane
 MouseInfo              mouse-position

Copyright (c) 2011 Jonathan Fischer Friberg

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.

3. This notice may not be removed or altered from any source distribution.

Something went wrong with that request. Please try again.