How can I create a JFrame with an image using Seesaw?

Hi!

I want to create a JFrame using Seesaw which

a) has JPanels on the left, right, top, and bottom, and
b) an image in the center.

Inspired by this example I wrote the following code:

(defn show-main-window
  []
  (let [content-pane (border-panel
                      :north (create-top-panel)
                      :west (create-left-panel)
                      :east (create-right-panel)
                      :south (create-bottom-panel)
                      :center (create-center-panel)

                      )

        f (frame :title "Title"
                 :content content-pane)]
    (native!)
    (-> f pack! show!)))

(defn create-top-panel
  []
  (label "create-top-panel"))

(defn create-left-panel
  []
  (label "create-left-panel"))

(defn create-center-panel
  []
  (let []
    (label
     :icon (clojure.java.io/resource "/Users/myusername/some-place-on-disk/img/2019_10_20_fireplace.jpg"))))

(defn create-right-panel
  []
  (label "create-right-panel"))

(defn create-bottom-panel
  []
  (label "create-bottom-panel"))

When I run this function the image is not visible.

How do I need to modify the code in order to display the image?

Thanks in advance

Pravles

You need to move your image into resources directory of your project.

Then call clojure.java.io/resource only with the name of the image: (clojure.java.io/resource "2019_10_20_fireplace.jpg").

Fixed code:

(defn show-main-window []
  (let [content-pane (border-panel
                       :north (label "create-top-panel")
                       :west (label "create-left-panel")
                       :east (label "create-right-panel")
                       :south (label "create-bottom-panel")
                       :center (label
                                 :icon (clojure.java.io/resource "2019_10_20_fireplace.jpg")))
        f (frame :title "Title"
                 :content content-pane)]
    (native!)
    (-> f
        pack!
        show!)))
1 Like

Thanks for your answer.

Is it possible to display an image from a completely different location on my disk (i. e. outside of resources)? If yes, how?

For example, with (File. "C:/Users/myusername/some-place-on-disk/img/2019_10_20_fireplace.jpg") (File is java.io.File).

Use (clojure.repl/doc seesaw.icon/icon) in REPL to see a list of all possible types for Icon.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.