0

I wrote this program in clojure to add background image to a JTable. I am stuck with the prepareRenderer function. It seems like clojure cannot handle the first parameter rd (equivalent Java TableCellRenderer rd). I might have done it all wrong here and I am very grateful if someone can help me resolve this problem. To run the program just save it to a tabletest.clj file and type at the clojure prompt:

(load-file "tabletest.clj")

If you comment out the prepareRenderer function you can run the code successfully without seeing the background image.

Please provide your own image file for the JTable background. The program is listed below:

    (import '(javax.swing JFrame JTable JPanel JScrollPane)
            '(javax.swing.table DefaultTableModel TableCellRenderer)
            '(java.awt Component BorderLayout Dimension Image))
    (import javax.imageio.ImageIO)
    (import java.io.File)

    (defn tabletest []
         (let [tableData (to-array-2d [
                       ["numbers" "67890" "This"]
                               ["mo numbers" "2598790" "is"]
                               ["got Math" "2598774" "a"]
                               ["got Numbers" "1234567" "Column"]
                               ["got pi" "3.1415926" "Apple"]
                                  ])
               colNames (to-array ["Col Labels" "Go" "Here"])
               frame (JFrame. "Table Example")
               panel (JPanel.)
               sP (JScrollPane.)
               dataModel (DefaultTableModel. tableData colNames)
               table (proxy [JTable] [dataModel]
                 (prepareRenderer [rd row col] ; problem starts here
                      (proxy-super rd row col) ; Don't know how to translate
                                                       ; Java code below to clojure:
                  (.setOpaque this false)  ;if (c instanceof JComponent)
                                ; ((JComponent)c).setOpaque(false);

                  this
                 )
                 (paintComponent [g2d]
                  ;(proxy-super paintComponent g2d)
                  (def image (ImageIO/read (File. "GreenCar.png")))
                  (.drawImage g2d image 0 0 this)
                  (proxy-super paintComponent g2d)
                         )
                    )
              ]

              (.setCellSelectionEnabled table true)
              (.setOpaque table false)
              (.setPreferredSize sP (Dimension. 300 150))
              (.setView (.getViewport sP) table)
              (.add panel sP)
              (doto frame
                  (.setLayout (BorderLayout.))
                  (.add panel BorderLayout/CENTER)
                  (.setSize 400 200)
                  (.setVisible true)
              )
          )
      )

      (tabletest)
Alex Miller
  • 69,183
  • 25
  • 122
  • 167

1 Answers1

0

The mistake is the line:

 (proxy-super rd row col)

The correct way to code it is:

 (def c (proxy-super prepareRenderer [rd row col]))

and the lines:

 (.setOpaque this false)
 this

to be replaced by:

 (if (instance? JComponent c)
     (.setOpaque c false))
 c
Griwes
  • 8,805
  • 2
  • 43
  • 70