The usual way to repeat, or tile a background image across and down a GUI component is to write nested loops in a paintComponent(…) override. TileIcon extends ImageIcon to provide this functionality and can fill the background of a component that can display an Icon.
Archive for the ‘Extensions’ Category
Posted by Darryl Burke on April 14, 2012
In the last two posts, we presented StretchIcon and ShrinkIcon, both used for scaling images to fit a component whose size is determined by the size and layout of its container. ThumbnailIcon extends ShrinkIcon to reduce an image when needed to fit within a defined size, padding and centering it horizontally or vertically as required. Like ShrinkIcon, this class does not magnify the image.
Posted by Darryl Burke on April 8, 2012
When writing about StretchIcon, published earlier, I mentioned that the image chosen for a StretchIcon should be large enough that it does not get magnified in fitting it to the component, since drawing an image larger than its natural size can lead to pixelation. ShrinkIcon extends StretchIcon to ensure such pixelation doesn’t happen.
Posted by Darryl Burke on March 31, 2012
An implementation of the Icon interface reports its size via the two methods getIconWidth() and getIconHeight(). It’s normal to expect that the Icon’s paintIcon(Component, Graphics, int, int) method will respect these bounds. If I had designed Swing’s interaction with Icons, I would have made sure of that by setting a clip to the Graphics passed to paintIcon(…).
Luckily for me, I didn’t design Swing, as that would have made StretchIcon impossible.
Posted by Darryl Burke on September 12, 2010
The default behavior of menus on all platforms, not just in Java, is for the menu to disappear when any item is selected. But once in a way, one comes across a situation where keeping the menu open could improve the user experience. For example, in the “Quick Preferences” submenu in Opera browser, a user who wants to set more than one option is forced to navigate the menu repeatedly.
Fortunately, the flexibility afforded in terms of extending the JDK classes makes this possible in Java.
Posted by Darryl Burke on August 29, 2010
It’s easy to make a JTable sortable by invoking setAutoCreateRowSorter(true). Clicking on the header of a column will then sort the rows on the basis of the contents of the cell in that column, and display an appropriate arrow icon. A little known fact is that the default, automatically created, RowSorter actually uses the last three sort indexes. Unfortunately, the only way the user can know which columns are involved in the subordinate sorting is by remembering the sequence in which the columns were clicked.
MultisortTableHeaderCellRenderer addresses this deficiency by showing the sort icons with decreasing opacity on all columns involved in the sort, making use of the AlphaIcon class published earlier.
Posted by Darryl Burke on April 18, 2009
The default, and usually desired, behavior of a scrollbar is to continuously scroll its associated component while the scroll thumb is being dragged. There are at least two situations where this behavior may lead to a sluggish and unresponsive GUI:
- A complicated custom renderer bogs down the processor
- The contents to display are dynamically loaded from disk as the portions of the scrollable component are shown.
JumpScrollBarModel provides a behavior akin to clearing the option “Show window contents while dragging” on Windows. The scrollable component’s position will remain fixed while dragging the scroll thumb, and will update when the drag is terminated by releasing the mouse button. An optional setting allows for updating the scroll position when the mouse is paused for a user-defined interval.
Posted by Darryl Burke on March 25, 2009
Discovering the hard way that the part of a JCheckBox or a JRadioButton that indicates its selected or rollover status is in fact the default icon of the component, and that invoking setIcon replaces the default icon with your custom icon, can be disconcerting or even exasperating.
CheckBoxIcon and RadioButtonIcon, which inherit from DualIcon, published earlier, through an abstract class ToggleButtonIcon, allow you to add a custom icon to these Swing components alongside the functional one.
Posted by Darryl Burke on March 6, 2009
Does your JTable have short column content, but long column names? Do the column headers take up more horizontal space than you can spare?
No longer do you need to resort to cryptic two and three letter column headings in an attempt to solve this problem. Just rotate your table header text to the vertical, using VerticalTableHeaderCellRenderer.
Posted by Darryl Burke on February 27, 2009
The javax.swing package and its subpackages provide a fairly comprehensive set of default renderer implementations, suitable for customization via inheritance. A notable omission is the lack of a default renderer for a JTableHeader in the public API. The renderer used by default is a Sun proprietary class, sun.swing.table.DefaultTableCellHeaderRenderer, which cannot be extended.
DefaultTableHeaderCellRenderer seeks to fill this void, by providing a rendering designed to be identical with that of the proprietary class, with one difference: the vertical alignment of the header text has been set to BOTTOM, to provide a better match between DefaultTableHeaderCellRenderer and other custom renderers.