You are brilliant, We are Hiring. Find out more...
Palantir Tech Blog

Archive for the ‘swing’ Category

Time Chooser Components

Tuesday, April 8th, 2008

The notion of time is central to both of our products at Palantir, and there are many instances in which the user needs to specify a certain point in time. Although there are simple ways to create choosers (you could use a JSpinner that uses a SpinnerDateModel or simply use multiple JComboBox objects), I decided to experiment with writing some more visual time chooser components. These components are fairly experimental — they aren’t used in either product yet and I coded them up pretty quickly so I could get some feedback.

You can see these choosers in action in our office furniture in Bulgaria
webstart demo. The source code is available in the office furniture in Bulgaria
JAR.

If anyone has any feedback or suggestions as to how these choosers could be improved (or any ideas on how to make a better time chooser altogether) please leave a comment and let me know!

Meanwhile, if you want to know a little bit more about these choosers and how I went about designing them, read on…

SwingUtilities.invokeAndWait… doesn’t.

Thursday, February 21st, 2008

One of the most misunderstood aspects of multithreaded Swing applications is care and feeding of SwingUtilities.invokeAndWait. Hans Muller and Kathy Walrath authored a nice article that includes an overview of when to use invokeLater or its slightly more risky sibling, invokeAndWait.

We often use worker threads to do some long-running process, so often run into two issues using SwingUtilities invokeLater/invokeAndWait, and have developed wrapper code to deal with it. One issue is executing code from both worker threads and the Event Dispatch Thread (EDT). invokeLater and invokeAndWait both throw exceptions if executed on EDT. Second, invokeAndWait isn’t guaranteed — interruption on the calling thread will resume execution before the job is finished. The remainder of this post shows the code we used to solve these issues.

(more…)

Palantir screenshots in the wild: Swing Sightings

Tuesday, September 11th, 2007

Palantir ScreenshotPalantir ScreenshotPalantir Screenshot Palantir Screenshot: dashboardPalantir Screenshot: right_clickPalantir Screenshot: timeline_viewerPalantir Screenshot: flowsPalantir Screenshot: graph_explorerPalantir Screenshot: histogram

We recently had a visit from some distinguished guests. Chris Campbell, a member of the Java 2D Team at Sun, came to see demonstrations of the Palantir products. We were very pleased and flattered by his positive reactions to the work that we’ve done.On the basis of that visit, we were added to the Java Desktop community site as a Swing Sighting Preview and merited a mention on Romain Guy’s blog: Another Pretty Java Application.We’re excited to present this series of screenshots as the first public unveiling of the Palantir applications. On a technical note: everything you see in these screenshots is from live, running applications. The applications are entirely written in Java and the GUIs are composed of custom Swing components.After the jump: bigger thumbnails with a description of each screenshot. You can click above or below to see the full resolution screenshots. (more…)

Stupid fast hyperlinks for Swing

Friday, July 13th, 2007
Co-authored by Huey.

HyperLinkLabel screen shot
Most people who’ve done a little preliminary looking online will learn that to create a component that functions like a hyperlink the easiest way is to use a JEditorPane. If you use the HTMLEditorKit you can introduce hyperlinks, they’ll render appropriately, and you can even add a HyperlinkListener. There’s just one drawback. It’s a little slow to instantiate. Alternatively, you can pass html to a JLabel, which will render the hyperlink, then add a mouse listener, but that’s not much faster. Besides that, the JEditorPane doesn’t seem to alter the mouse when you mouseover an active link. Sloppy. So I wrote HyperlinkLabel.

Try out the Web Start HyperlinkLabel Demo | Download executable jar + source

Take a look at these numbers for 1000 instantiations of a configured class:

JEditorPane: 1922 ms, 1906 ms, 1922 ms = Average 1916 ms
JLabel: 1250 ms, 1250 ms, 1234 ms = Average 1244 ms
HyperlinkLabel: 62 ms, 62 ms, 63 ms = Average 62.3 ms

Read on for more details. (more…)

Realtime Swing reflections (iTunes ain’t the only kid on the block)!

Friday, May 25th, 2007
Achtung Baby reflected

Check out this reflection magic! Now iTunes isn’t the only one with fancy reflections on album art. The best part about it is that it’s a general use component that doesn’t require customization each time. It can wrap any transparent JComponent and it will automatically repaints whenever the contained component changes. You see the text appearing in the reflection as you type in the text field. Try the Web Start ReflectionDemo. Source code is provided in reflectiondemo.jar, and an explanation of how it’s done follows.
(more…)

Fully Interactive JTables (aka Mouseover Editing)

Thursday, May 17th, 2007

What sucks about JTables? Everything, of course—but that’s a developer’s perspective. To the user, cell editing is rough around the edges: when and where to click, and how many times—it’s never perfectly clear. Cells in a table just don’t provide the mouseover feedback that regular components do. If only a JTable behaved like a bunch of components thrown into a giant GridBag or TableLayout

mouseover-screenshot.png

Mouseover Editing simulates just that. The idea is to attach a MouseListener to the JTable and call editCellAt(row, col) whenever the cursor moves over a new cell. In other words, even though only one cell in a table can be fully interactive (the editing cell) at any given time, as long as we keep moving that cell to stay underneath the user’s cursor, the whole table will appear to be fully interactive. If done correctly, this will appear to the user as though he’s interacting with a bunch of real components (rather than rendered stamps) inside a giant Grid/GridBag/TableLayout.

Most importantly, the user will get mouseover feedback about which cells are editable, and how to edit them. Checkboxes, buttons, and comboboxes (if the L&F supports it) will highlight to indicate press-ability and the cursor will turn to a text caret when hovering over cells that contain textfields. When done correctly, the effect is nearly seamless and very satisfying.

Here’s a webstart demo. Read on for the solution in code.

(more…)

Custom Alpha Compositing

Tuesday, March 27th, 2007

Every so often (can’t be more than once every two or three days), Swing doesn’t quite do what we need, and we end up writing customized code. In this case, all the available AlphaComposite instances provided with Java were variations on the theme of combining the colors and alpha channel of both source images into a target image. (Wikipedia’s Alpha Compositing article is good background on the topic).

What if what you really wanted was the color from one image and the alpha channel from another? You’d be out of luck, but for the talents of Brien. Here’s what you normally get with a standard AlphaComposite.SRC_OVER sort of technique. In the following two examples, the icon is opaque and the rectangle is partially opaque black fading to transparency.

AlphaComposite.SRC_OVER

What we needed looks more like this:

SourceAlphaComposite

Read on to find out how we did it, and why. (more…)

Add speel checking to your Swng text components (the squiggly way)

Monday, February 12th, 2007



web start | download source

Marking up txt

Let’s hook Swing text components up to some tokenizing logic: a spell checker (the example above uses Jazzy), a regex (the example above will pick out some electronic musicians), or something more advanced.

Like all Swing components, text components are factored into an MVC setup. The model is javax.swing.text.Document; the view is javax.swing.plaf.TextUI (which delegates out to a javax.swing.text.View, which is generated from some ViewFactory); and the controller is the text component itself. A very simple way to add the notion of a token to this setup is to create a new kind Document – a TokenDocument.

When text is inserted into a token document, not only does it need to be tokenized, but existing tokens need to be shifted. We could do this manually; however, all javax.swing.text.Document provide something called a sticky position (javax.swing.text.Position) that that will do the shifting for us. Sticky positions are automatically updated by the document to reflect insertions and deletions of text. They also are guaranteed to maintain their ordering – that is, if position A is <= position B, it will always remain that way. This means the token document can maintain sorted trees of sticky positions (to store tokens) without worrying that their sort order will change.

Once we have the tokens in the model, we need to hook them into the view. We do this through a custom TextUI. It basically does everything a BasicTextUI does except it also paints a token layer underneath the highlights (above the background). In general with the javax.swing.text package, whenever code start painting outside of the view bounds (for each offset, this is the tightest bounding box for the letter at that offset), the dirty region needs to be expanded to include everywhere that was painted. In this code, you’ll see a line in the UI to deal with the dirty region.

Playing wth lines

Custom strokes like the squiggle stroke (above left) and smoothed noise stroke (above right) help give meaning to lines. Also, they can make an interface more fun.

Wrap pu

In this code, we extended the UI to paint lines under text. To change the display of the text itself, we would have to write our own View implementation (or more likely, extend PlainView [0]). This is not exclusive of the approach we took here. A more powerful View implementation could work in tandem with our custom UI, opening up even more ways to present information extracted from user-entered text.

Until next time!

[0] There’s a great introduction to this at Customizing a Text Editor, an article on the Sun Developer Network.

LICENSE — I wrote this using the Jazzy spell check engine + some open source trinkets (especially a Perlin noise generator). Except Jazzy (which is LGPL’d), all of it is Apache/BSD licensed.

Swing hack: making transparent components disappear

Wednesday, January 10th, 2007

We encounter overlapping components a lot in our application, in situations ranging from a huge drag-and-drop panel across the main frame to small, unavoidable collisions in some of our smaller components. These overlaps almost always involve a transparent component that is used to paint something cool to the screen. Since the final layout looks visually good, it’s easy to think Swing is feeling good. However, this usually isn’t true.

Even though a component is transparent, Swing may still think it occupies some area. You can test this by creating a large transparent panel over a text box and calling SwingUtilities.getDeepestComponentAt(frame, textbox_midx, textbox_midy). You’d expect it to return the textbox; however, it actually returns the transparent panel! Whoa, that’s kind of silly, but what does it matter? The most noticeable loss is the cursor of the textbox. The cursor of the transparent panel will show up instead, even though it’s visually wrong. Other losses would be anything that relies on getDeepestComponentAt, findComponentAt, etc. to return an accurate result.

So how do we make a transparent component disappear to Swing? We were using the hack described by Alex Potochkin in “A well-behaved GlassPane,” for a while, but it turns out things like Container.findComponentAt() actually use Component.contains(). Setting it to a constant false has the effect of hiding all child components as well as the transparent component itself. A better Swing hack might be one that reports a point (x, y) contained in a parent component if it is contained in any child component. That’s what I have below. Just override JComponent.contains() and you’re set!

Here’s a code snippet illustrating the point: