public
Last active

miglayout swing clojure experiments

  • Download Gist
swing.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
(ns swinger
(:import (java.awt BorderLayout Container Dimension))
(:import (javax.swing JButton JFrame JLabel JPanel JTextField JOptionPane JScrollPane JList ImageIcon JComboBox JSeparator JTable UIManager SwingUtilities AbstractButton JFileChooser JDialog JProgressBar JTabbedPane))
(:import (javax.swing.table AbstractTableModel))
(:import (java.awt.event MouseAdapter MouseListener KeyEvent))
(:import (java.awt Toolkit BorderLayout Dimension Color Dialog$ModalityType))
(:use (clojure.contrib
[miglayout :only (miglayout components)]
[swing-utils :only (add-action-listener add-key-typed-listener make-menubar)])))
 
(defn border-example []
(let [frame (JFrame. "border example") pane (.getContentPane frame) ]
(add-components-to-pane pane)
(.pack frame)
(.setVisible frame true)))
 
(defn add-components-to-pane
[pane]
(let [page-start (JButton. "page start")
center (JButton. "center")
line-start (JButton. "line start")
page-end (JButton. "page end")
line-end (JButton. "line end") ]
(do
(.setComponentOrientation pane java.awt.ComponentOrientation/RIGHT_TO_LEFT)
(doto pane
(.add page-start BorderLayout/PAGE_START)
(.add center BorderLayout/CENTER)
(.add line-start BorderLayout/LINE_START)
(.add page-end BorderLayout/PAGE_END)
(.add line-end BorderLayout/LINE_END))
(.setPreferredSize center (Dimension. 200 100)))))
 
(defn handler
[event parent]
(JOptionPane/showMessageDialog
parent (str "Button " (.getActionCommand event) " clicked.")))
 
(defn click-me
[]
(let [ panel (miglayout
(JPanel.)
(JButton. "Click me!") { :id :click-me }) ]
(add-action-listener (:click-me (components panel)) handler panel)
(doto (JFrame.)
(.add panel)
(.setPreferredSize (Dimension. 800 600))
(.setResizable false)
(.setLocationRelativeTo (:click-me (components panel)));
(.pack)
(.setVisible true))))
 
(comment (click-me))
 
;; JLabel emptyLabel = new JLabel("");
;; emptyLabel.setPreferredSize(new Dimension(175, 100));
;; frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
 
(defn test-frame
[]
(let [frame (JFrame. ) label (JLabel. "Hello world")]
(.setPreferredSize frame (Dimension. 800 600))
(.add (.getContentPane frame) label BorderLayout/CENTER)
(.setBackground (.getContentPane frame) Color/gray)
(.setLocation frame 300 180)
(.pack frame)
(.setVisible frame true)))
 
(comment (test-frame))
 
(def msg-type { :info JOptionPane/INFORMATION_MESSAGE
:error JOptionPane/ERROR_MESSAGE
:warn JOptionPane/WARNING_MESSAGE
:message JOptionPane/PLAIN_MESSAGE
})
 
(defn- dialog
[parent msg type]
(JOptionPane/showMessageDialog parent msg type (msg-type (keyword type)) (ImageIcon. "/home/abhi/downloads/Firefox_wallpaper.png")))
 
(defn- dialog
[parent msg type]
(JOptionPane/showMessageDialog parent msg type (msg-type (keyword type)) ))
 
(defn info [parent msg]
(dialog parent msg "info"))
 
(defn alert [parent msg]
(dialog parent msg "error"))
 
(defn warn [parent msg]
(dialog parent msg "warn"))
 
(defn message [parent msg]
(dialog parent msg "message"))
 
(defn ask
[msg]
(let [frame (JFrame.)]
(dialog frame msg "warn" )))
 
(def my-table-model
(let [ column-names ["File" "Ranking" ]
column-data [["a.txt" 10] ["b.txt" 2]]
row-count 5 ]
(proxy [AbstractTableModel] []
(getColumnCount [] (count column-names))
(getRowCount [] row-count)
(getValueAt [i j] (get-in column-data [i j]))
(getColumnName [i] (column-names i)))))
 
(defn show-open-dialog
[event frame]
(JOptionPane/showMessageDialog (JPanel.)
(let [chooser (JFileChooser.)]
(.setFileSelectionMode chooser JFileChooser/DIRECTORIES_ONLY)
(let [ ret (.showOpenDialog chooser frame)]
(.setFileSelectionMode chooser JFileChooser/DIRECTORIES_ONLY)
(str (cond
(= JFileChooser/APPROVE_OPTION ret) (.getSelectedFile chooser)
(= JFileChooser/CANCEL_OPTION ret) "canceleshwar"
:else "error"))))))
(defn menubar
[frame]
(make-menubar [{:name "Index"
:mnemonic KeyEvent/VK_I
:items
[{:name "Add Directory" :mnemonic KeyEvent/VK_A :short-desc "New" :long-desc "Start " :handler (fn [_] (show-open-dialog nil frame)) }] }]))
 
 
(defn double-click? [event]
(= 2 (.getClickCount event)))
 
(defn add-mouse-listener
[component]
(let [listener (proxy [MouseAdapter] []
(mouseClicked [event]
(and (double-click? event) (SwingUtilities/isLeftMouseButton event)
;(info (JPanel.) (str "row " (.getAnchorSelectionIndex (.getSelectionModel component)) " selected"))
(info (JPanel.) (str (.getValueAt component (.getSelectedRow component) (.getSelectedColumn component))))
)))]
(.addMouseListener component listener)
listener))
 
;; JTable code
(defn editable-table
[]
(let [ panel (miglayout
(JPanel.)
(JScrollPane. (JTable. (to-array-2d [[1 2 3] [4 5 6] [7 8 9]]) (into-array ["a" "b" "c"])))
) ]
(doto (JFrame. "demo")
; (.setJMenuBar menubar)
(.add panel)
(.setLocation 300 180)
(.pack)
(.setVisible true))))
 
(comment (editable-table))
 
(defn non-editable-table
[]
(let [ jtable (JTable. my-table-model)
panel (miglayout
(JPanel.)
(JScrollPane. jtable)) ]
(add-mouse-listener jtable)
(doto (JFrame. "demo")
; (.setJMenuBar menubar)
(.add panel)
(.setLocation 300 180)
(.pack)
(.setVisible true))))
 
; http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#data
(non-editable-table)
 
;; aTable.addMouseListener(new MouseAdapter(){
;; public void mouseClicked(MouseEvent e){
;; if (e.getClickCount() == 2){
;; System.out.println(" double click" );
;; }
;; }
;; } );
;; }
 
;; http://www.rgagnon.com/javadetails/java-0336.html
;; http://java.sun.com/docs/books/tutorial/uiswing/events/mouselistener.html
 
;;(UIManager/setLookAndFeel (UIManager/getSystemLookAndFeelClassName))
(defn cxr-ui
[]
(let [ jtable (JTable. my-table-model)
panel (miglayout
(JPanel.) :column "[]"
(miglayout (JPanel.)) {:id :search-panel } :wrap
(JScrollPane. jtable) {:id :result-panel :height 400 :width 760 :gapleft 5 }
(miglayout (JPanel.)) {:id ::status-panel } )
frame (JFrame. "demo") ]
(doto (:search-panel (components panel))
(.add (JComboBox. (into-array (sort [" search 1" " search 2" " search 3"]))) "w 100")
(.add (JTextField. "Enter text") "h 22, w 300, gapleft 20")
(.add (JButton. "search") ""))
(doto (.getColumn (.getColumnModel jtable) 1)
(.setPreferredWidth 100)
(.setMinWidth 100)
(.setMaxWidth 100))
(doto jtable
; (.setAutoResizeMode JTable/AUTO_RESIZE_OFF)
(.setShowGrid false)
(.setShowHorizontalLines true)
(.setShowVerticalLines true)
(.setGridColor Color/black))
(add-mouse-listener jtable)
(doto frame
; (.setJMenuBar (menubar frame))
(.setLocation 300 180)
(.setResizable false)
(.pack)
(.setVisible true))))
 
(comment (cxr-ui))
;; http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JList.html
;; TODO - figure out how to fetch JTable object from (miglayout ... (JScrollPane. (JTable.)))
 
(defn click-me
[]
(let [ panel (miglayout
(JPanel.)
(JButton. "Click me!") { :id :click-me }) ]
(add-action-listener (:click-me (components panel)) show-open-dialog panel)
(doto (JFrame.)
(.add panel)
(.setPreferredSize (Dimension. 400 300))
(.setResizable false)
(.setLocationRelativeTo (:click-me (components panel)));
(.pack)
(.setVisible true))))
 
(comment (click-me))
 
(defn modal
[event parent]
(doto (JDialog. parent "hello world" true)
(.setPreferredSize (Dimension. 800 600))
(.setLocation 300 180)
(.add (JLabel. "hello world"))
(.pack)
(.setVisible true)))
;; (JOptionPane/showMessageDialog
;; parent (str "Button " (.getActionCommand event) " clicked.")))
 
(defn modal-demo
[]
(let [ panel (miglayout
(JPanel.)
(JButton. "Click me!") { :id :click-me })
frame (JFrame.) ]
(add-action-listener (:click-me (components panel)) modal frame)
(doto frame
(.add panel)
(.setPreferredSize (Dimension. 800 600))
(.setResizable false)
(.setLocationRelativeTo (:click-me (components panel)));
(.pack)
(.setVisible true))))
 
(comment (modal-demo))
 
;Dialog$ModalityType/DOCUMENT_MODAL
 
;; progress-bar
(defn progressbar
[]
(let [ panel (miglayout (JPanel.))
dialog (JDialog.)
pbar (JProgressBar. 0 100) ]
(.add panel pbar)
(doto dialog
(.setModal true)
(.add panel)
(.setLocationRelativeTo nil)
(.pack)
(.setVisible true))))
 
(def iwindow-table-model
(let [ column-names ["Directory" "Indexed" ]
column-data [["a.txt" "No"] ["b.txt" "Yes"]]
row-count 5 ]
(proxy [AbstractTableModel] []
(getColumnCount [] (count column-names))
(getRowCount [] row-count)
(getValueAt [i j] (get-in column-data [i j]))
(getColumnName [i] (column-names i)))))
 
(defn iwindow
[frame]
(let [ jtable (JTable. iwindow-table-model)
panel (miglayout
(JPanel.)
(miglayout (JPanel.)) {:id :add-panel } :wrap
(JScrollPane. jtable) {:id :result-panel :height 400 :width 360 :gapleft 5 })
dialog (JDialog.)
button (JButton. "Add")]
(add-action-listener button show-open-dialog panel)
(.add (:add-panel (components panel)) button )
(doto dialog
(.add panel)
(.setLocation 300 180)
(.setLocationRelativeTo frame)
(.pack)
(.setVisible true))))
 
 
(defn tabbbedpane
[]
(let [ tpane (JTabbedPane.)
frame (JFrame.) ]
(.addTab tpane "a" (miglayout
(JPanel.)
(JScrollPane. (JTable. (to-array-2d [[1 2 3] [4 5 6] [7 8 9]]) (into-array ["ha" "ha" "ha"])))))
(.addTab tpane "b" (miglayout
(JPanel.)
(JScrollPane. (JTable. (to-array-2d [[1 2 3] [4 5 6] [7 8 9]]) (into-array ["a" "b" "c"])))))
(.setPreferredSize frame (Dimension. 800 600))
(.add frame tpane)
(.setVisible frame true)))
 
;(def column-data (ref []))
(def column-names ["Filename"])
(def file-agent (agent []))
 
(def running (ref nil))
 
(def my-dynamic-table-model
(proxy [AbstractTableModel] []
(getColumnCount [] (count column-names))
(getRowCount [] (count @file-agent))
(getValueAt [i j] (get-in @file-agent [i j]))
(getColumnName [i] (column-names i))))
 
(add-watch file-agent :file-agent
(fn [k r o n]
; (info (JPanel.) @file-agent)
(.fireTableRowsInserted my-dynamic-table-model 0 0)))
 
(defn listeshwari [x lst]
(if (and @running (not-empty lst))
(do (send *agent* listeshwari (rest lst))
(Thread/sleep 1000)
(conj @file-agent [(.getName (first lst))]))
@file-agent))
 
(defn populate-table
[event x]
(let [dir (.getText x)]
(info (JPanel.) dir)
(dosync (ref-set running true))
(send file-agent listeshwari (file-seq (java.io.File. dir)))))
 
(defn clear-table [event]
(do
(dosync
(ref-set running false))
(send file-agent (fn [x] []))
(.fireTableRowsDeleted my-dynamic-table-model 0 0)))
 
(defn dynamic-non-editable-table
[]
(let [ panel (miglayout
(JPanel.)
(JTextField. 20) {:id :search-key}
(JButton. "populate table") { :id :p-button } :span
(JButton. "clear table") { :id :c-button } :wrap
(JScrollPane. (JTable. my-dynamic-table-model)))
{:keys [p-button c-button search-key] } (components panel) ]
(add-action-listener p-button populate-table search-key)
(add-action-listener c-button clear-table)
(doto (JFrame.)
(.add panel)
(.setResizable false)
(.pack)
(.setVisible true))))
 
(dynamic-non-editable-table)
 
(comment
(def foo (agent nil)) ;; agent used purely for side effect
(def bar (ref []))
(add-watch bar :bar (fn [k r o n] (info (JPanel.) (last @bar))))
(send foo (fn [e]
(do (doseq [f (file-seq (java.io.File. "/home/abhi/Docs/"))]
(Thread/sleep 1000)
(dosync
(alter bar conj (.getName f))))))))
 
(defn change-label
[event]
(let [ button (.getSource event) label (.getLabel button) ]
(.setLabel button (if (= label "on") "off" "on"))))
 
(defn toggle
[]
(let [ panel (miglayout
(JPanel.)
(JButton. "on") { :id :on }) ]
(add-action-listener (:on (components panel)) change-label)
(doto (JFrame.)
(.add panel)
(.pack)
(.setVisible true))))
 
;(toggle)
;(cxr-ui)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.