I'd like to create a simple Swing app. I've got very, very, very little experience with Swing, however. I want to create a one window app that refreshes every 5 minutes with the contents of a screen-scraping that I do. I'm using Clojure to write the code. I assume Swing is the way to go with this, but if there are other, better options I'd love to hear more about those as well.
What code would I need to do this with Swing? (what classes should I use, etc)
Thanks,
Alex
Well, for the every five minutes bit, java.util.TimerTask should be of help. For general Swing information, this link to the Java Tutorials ought to help.
To have a Window, specifically, JFrame is probably your best bet.
To display single or multiline text, you ought to look into JLabel or JTextArea, respectively.
To display images, ImageIcon ought to do the trick.
For other needs, the Java Tutorial ought to be a big help.
As trashgod suggested, javax.swing.Timer has some advantages when it comes to GUIs over java.util.TimerTask. This article on using timers in Swing applications should help you decide which to use.
You're right. Swing is the way to go, but connecting all the pieces can be a bit tough if you're learning Clojure and Swing. There are a few short examples floating around showing how to create simple Swing GUIs in Clojure. Here's another short example that combines a simple GUI 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))
When run, this will create a small window with a label that is updated every second. From your description, you would change the frequency of the timer from 1,000ms to 300,000ms to trigger an action every 5 minutes. To do something other than updating a label, you would change the contents of the timer-action function.
I think this is thread safe, but haven't checked for sure. There are cautions and tutorials about thread safety when updating Swing components too. You'll probably want to check those too.
I hope this is informative enough to give yo a few clues as to where to look for further information.
EDIT: I wanted to point out one more interesting thing here. Note that the 'timer-action' function is changing the value of one of its arguments. The 'counter' argument is an atom defined in 'timer-fn', yet the action listener is able to change it. This is something you cannot usually do in Java. Maybe someone smarter than me can comment on whether this constitutes a "closure". In my previous experience with languages like Pascal, I would say the argument passing is "call-by-reference" as opposed to Java's strict "call-by-value" argument passing. Is this something different?
EDIT 2: After checking my facts with another question, this is, in fact, an example of a closure in Clojure.
In a Swing context, javax.swing.Timer has some advantages; there's an example here. Depending on what you want to display, JEditorPane may be appropriate.
On top of the resources mentioned by #Zach L (particularly regarding the timers), I would take a good look at Seesaw, especially since you're writing this in Clojure.
In particular, I note the seesaw.timer for firing the refresh events. Using a JTextPane (read-only) or a JEditorPane (editable) would work well for displaying richly formatted results (like HTML).
Try this link for Swing. As Zach said you will need to use JFrame and TimerTask should be used for your requirements.
You can also try other alternative frameworks for Swing.
Clojure's software transactional memory allows you to set watches on variables; your callback is executed whenever the variable is changed (by anything). This lends itself very well to GUI programming. Your GUI can auto-update whenever anything touches the variable.
Here is a short but non-trivial example of how to do this, with explanation of what is going on: http://www.paullegato.com/blog/swing-clojure-gui-black-scholes/
Related
Any parameter to set A JFrame's border/frame thickness or existence and still keep the title bar intact? I want an almost borderless frame with a thin blue line like this one and not like the default border.
If JFrame isn't the way to go, what is a good way to achieve that? (preferably that is compatible with WindowBuilder but that's probably asking for too much).
A search barely yields any mention and related questions on SOF don't seem to have answers so I thought I'd try to get a good answer once and for all.
JFrame#setUndecorated
Disables or enables decorations for this frame.
This method can only be called while the frame is not displayable. To make this frame decorated, it must be opaque and have the default shape, otherwise the IllegalComponentStateException will be thrown. Refer to Window.setShape(java.awt.Shape), Window.setOpacity(float) and Window.setBackground(java.awt.Color) for details
Please, consult the available documentation
Please note, you will become responsible for providing the title bar yourself, should you want it
A search barely yields any mention and related questions on SOF don't seem to have answers
Google provides a number of promising hits
I ended up switching to NetBeans and learning some Photoshop basics which you'll need thanks to a comment by #MadProgrammer
writing your own look and feel delegate
and ended up exactly with what you mentioned #theProgrammer101
You can make a JButton, and when it is clicked, call System.exit(0) , which will terminate the program
You can create a similar button for minimize action as well as your own drop down menus that are totally custom made and you won't need to rely on the default JFrmae window in case that bothers you too (I found it horrid).
check out this link for a good NetBeans tutorial with an nice example of writing your own look and feel delegate and this link for a great tutorial on getting started with Photoshop which is critical to GUI creation.
Thought i'd round up some of my research for anyone else who's just getting into GUI's.
I'm working on one of my first java applets and I want to start of fairly simple (though I have a good understanding of how code works I dont know much in terms of what methods I all have at my disposal when using java)
I have created a Jframe window that has a JTextarea in it. I would like to execute certain lines of code when certain things are typed into this box. In essence, its a simple text input system. How would I go about doing this or is there a better component to use for this?
in addition to getText(), for JTextField some prefer the getDocument() method. In Java, Listeners are used to capture events, such as "what was typed to the text area". This tutorial will get you started, if you have trouble implementing you can come back with a more specfic question and some code :)
I don't have much experience in Java, but I am attempt to write a simple rogue-like game to familiarize myself with it, and I am just wondering how I would go about creating an interface like this:
Are there any obvious ways that you would go about something like this? Being new to Java, I really have no idea what the best method would be.
Sorry to be vague!
Thanks
There is no such (simple) component in the JDK - if you don't need color, a JTextArea can be used to display ASCII-Art (after setting a fixed-width font). You will need to take care not to run into characterset issues (if you don't stick to US-ASCII 7-bit).
Writing a component that handles color display (and maybe escape sequences, in essence emulates a console window) wouldn't be too hard, but if you just started with Java it may prove to be an unwelcome challenge.
You could also just write your game in Java and leave displaying the ASCII to the system console (your game would simple output to stdout).
Edit: Color ASCII could be acieved by converting your internal format to (simple) HTML and that HTML could be displayed using a JLabel. Its probably not the most elegant method, but it should be reasonably simple to implement (and with nowadays hardware speed should not be an issue with this approach either). This approach builds on the capability that you can just use JLabel.setText() and pass a string that starts with a HTML tag. The JLabel then interprets the whole text as HTML.
Check out Trystan's Ascii Panel, and his blog and tutorial on making a roguelike here.
Better late than never, right? You may want to check Zircon Project.
I need a multi-value JSlider (or a similar component) for an analysis application. The two features missing from the regular JSlider are the ability to have more than one knob and also the ability to add or remove knobs on the fly. The reason for this is that they will be used to partition the 0..100% range for a particular factor into two or more subranges which are fed into a binning algorithm.
After some unsuccessful googling, it seems I'll have to develop a custom component (which I'm not very good at, I've been coding in Java for 10 years, but have zero experience in Swing). Is it possible to extend (easily :-) the JSlider component? Or are there better alternatives, perhaps not Swing-based but web-based? I have some flexibility in selecting the GUI approach for this. The current analysis application is command-line so a Swing GUI would be most straighforward, but nothing really prevents me for turning it into a web app if need be.
Thank you!
Take a look at JXMultiThumbSlider in SwingX.
I'm using the Flamingo ribbon and the Substance Office 2007 look and feel.
Of course now every control has this look and feel, even those on dialog boxes.
What I want is something like in Office 2007, where the ribbons have their Office 2007 look, but other controls keep their native Vista/XP look.
Is it possible to assign certain controls a different look and feel? Perhaps using some kind of chaining or a proxy look and feel?
I just discovered: Since Substance 5.0 the SKIN_PROPERTY is available.
It allows assigning different skins to different JRootPanes (i.e. JDialog, JFrame, JInternalFrame)
A little trick: I override JInternalFrame to remove the extra border and the title pane so that it looks just like a borderless panel. That way it is possible to create the impression, that different parts of a form/dialog have different looks.
Here is a library which will automaticaly change the look and feel. I am not sure it this will done for every component in a different way, but you should take a look at it. pbjar.org
This book should be useful if you want to go deep into look and feel /java-look-and-feel-design-guidelines-second-edition
I would be glad to see some code example, if someone can write it, feel free to get starting.
EDIT:
In this forum thread Thread i found the following description
Swing uses a Look & Feel (a PLAF).
PLAFs aren't attached on a per-JFrame
level. They are attached on a per-VM
level. It is almost impossible to mix
PLAFs within one application. I have
seen a few attempts, all failed.
Swing unfortunately does lots of "psuedo-global" things behind the scenes. AFAIK, the only way to do it consistently is to use the private AppContext API. Each AppContext has its own event dispatch thread and other "psuedo-globals".