Beginner to Swing

I would like to create a simple Swing application. However, I have very, very, very little experience with Swing. I want to create a one-window application that updates every 5 minutes with the contents of the screen I am making. I use Clojure to write code. I guess Swing is the way to go with this, but if there are other, better options that I would like to hear more about it.

What code do I need with Swing? (which classes should be used, etc.)

Thanks Alex

+8
java clojure swing
source share
6 answers

Well, for every five minutes, the java.util.TimerTask bit should help. For general Swing information, this link to the Java Tutorials should help.

To have a window in particular, a JFrame is probably best.

To display single or multi-line text, you must look into JLabel or JTextArea , respectively.

To display images, ImageIcon must do the trick.

For other purposes, a Java tutorial should be of great help.

Like trashgod , javax.swing.Timer has some advantages when it comes to the graphical user interface via java.util.TimerTask. This article on using timers in Swing applications should help you decide what to use.

+8
source share

You're right. Swing is the way to go, but joining all parts can be a little tough if you are learning Clojure and Swing. There are a few short examples that show how to create simple Swing GUIs in Clojure. Here is another short example that combines a simple graphical interface with a Timer object.

 (ns net.dneclark.JFrameAndTimerDemo (:import (javax.swing JLabel JButton JPanel JFrame Timer)) (:gen-class)) (defn timer-action [label counter] (proxy [java.awt.event.ActionListener] [] (actionPerformed [e] (.setText label (str "Counter: " (swap! counter inc)))))) (defn timer-fn [] (let [counter (atom 0) label (JLabel. "Counter: 0") timer (Timer. 1000 (timer-action label counter)) panel (doto (JPanel.) (.add label))] (.start timer) (doto (JFrame. "Timer App") (.setContentPane panel) (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE) (.setLocation 300 300) (.setSize 200 200) (.setVisible true)))) (defn -main [] (timer-fn)) 

At startup, this will create a small window with a label updated every second. From your description, you will change the timer frequency from 1000 ms to 300 000 ms to activate the action every 5 minutes. To do something other than update the label, you change the contents of the timer-action function.

I think it is thread-safe, but did not check for sure. There are warnings and thread safety guides when upgrading Swing components. You probably want to check them out as well.

I hope this is informative enough to give some clues as to where to look for additional information.

EDIT . I would like to mention one more interesting thing. Note that the timer-action function changes the value of one of its arguments. The counter argument is an atom defined in timer-fn, but the action listener can change it. This is something you usually cannot do in Java. Maybe someone smarter than me can comment on whether this is a "closure". In my previous experience with languages ​​like Pascal, I would say that passing an argument is a "call by reference", as opposed to passing strict Java arguments "call-by-value". Is it something else?

EDIT 2 : after checking my facts with another question, this is actually an example of closure in Clojure.

+4
source share

In the context of Swing, javax.swing.Timer has some advantages; here is an example here . Depending on what you want to display, JEditorPane may be appropriate.

+2
source share

In addition to the resources mentioned in @Zach L (especially with respect to timers), I would have a good look at Seesaw , especially since you are writing this in Clojure.

In particular, I mark seesaw.timer to trigger update events. Using JTextPane (read-only) or JEditorPane (editable) will work well to display highly formatted results (e.g. HTML).

+1
source share

Try the link for Swing. As Zack said, you will need to use a JFrame, and TimerTask should be used for your requirements.

You can also try other alternative frameworks for Swing.

0
source share

Clojure transactional memory software allows you to set the clock to variables; your callback is executed whenever the variable changes (by nothing). This is very suitable for GUI programming. Your GUI can automatically update whenever something touches this variable.

Here is a short but non-trivial example of how to do this, with an explanation of what is happening: http://www.paullegato.com/blog/swing-clojure-gui-black-scholes/

0
source share

Source: https://habr.com/ru/post/651334/


All Articles