[Java] Specifying the column widths of a JTable as percentages

The widths of the columns of a JTable are specified through TableColumn.setWidth. To set the widths of the columns as percentage, set the widths of column in proportion to each other. For example:

Read more of this post

[Java] Getting Combo Box Renderers to Look Consistent with Other Combo Boxes

One way of controlling the text in a JComboBox is to provide a custom ListCellRenderer. I have seen implementations that subclass a concrete implementation (such as DefaultListCellRenderer or the BasicComboBoxRenderer). Generally though, I find that the combo boxes using such renderers can appear inconsistent with another combo box that may not have a renderer set or one that extends a different implementation. To illustrate the inconsistency that could happen, I prepared a small test program that you can download from my Github repository. It displays a number of combo boxes that are based on different renderers. Just running it produces the following dialog in Windows 7:

Read more of this post

[Java] Swing Preview Popups

Sometimes in a JTabbedPane, it is nice to show a preview of the contents of tab in the background. This is similar to the windows preview that is available in a number of modern desktop environemnts (such as Windows and Gnome with Compiz). However, the preview popup is not a standard feature in Swing. Here, we look at how this feature may be added to the JTabbedPane.
Read more of this post

[Jung] Detecting Selection of Vertices in Jung

In Jung, a PickedState object provides information about which vertices have been selected. To receive notifications when verticesare selected or unselected, define an ItemListener and add it to the PickedState via addItemListener. The listener is notified whenever an item is selected or unselected (or, if you prefer the terms, picked or unpicked).

Read more of this post

[Jung] Finding Vertices on a Graph in Jung

Finding the vertex or edge at a specific point, or the set of vertices in a certain area, requires the graph’s pick support. The support is obtainable from VisualizationViewer‘s getPickSupport method. The support is actually an implementation of GraphElementAccessor and contains the following methods:

    V getVertex(Layout<V,E> layout, double x, double y);

    Collection<V> getVertices(Layout<V,E> layout, Shape rectangle);

    E getEdge(Layout<V,E> layout, double x, double y);

Read more of this post

[Jung] Controlling the Appearance of Vertices and Edges in Jung

In Jung, if you want to control of the vertices or edges, you need to provide the appropriate Transformer to the RenderContext in the BasicVisualizationServer. From the Transformer‘s documentation it is simply an interface that is “implemented by classes that transform one object into another“. In Jung the Transformer “transforms” a vertex or edge to a colour, shape, text or something else that controls their appearance.

Read more of this post

[Java] Making Fields Focusable in a JPopupMenu

Custom Swing popups are often created using a JPopupMenu. Some custom popups may contain input fields, such as a JTextField or a JSpinner. For these fields to be able to gain focus when the popup (or popup menu) is displayed, the invoker should be set prior to making it visible with setVisible. The invoker is usually the component that the user has to interact with in order to cause the popup to show. The invoker is set by calling the popup menu’s setInvoker.

Read more of this post