Java Tips Weblog

  • Blog Stats

    • 2,571,461 hits
  • Categories

  • Archives

Swing Utils

Posted by Darryl Burke on November 13, 2008

The standard SwingUtilities class has almost too many methods to count, including 4 that return various ancestors of a component.  Alas, the only method of some use to obtain a descendant, getDeepestComponentAt(…) is useful only in the context of a MouseListener.

SwingUtils attempts to provide methods for obtaining a reference to nested components, together with some for obtaining the properties and UIDefaults pertaining to a component.

The methods of the class fall in four broad groupings:

  • getDescendant(s)OfType return component(s) of a specified class including component(s) of named or anonymous subclasses.
  • getDescendant(s)OfClass return component(s) of the specified class only
  • getUIDefault(s)OfClass return a subset of the UIDefaults for a particular class or a single value for a particular property.
  • getJClass returns the nearest javax.swing superclass of a derived component, getProperties returns a Map of non-null, human-readable properties of a component and equals determines whether two objects are either equal or both null.

The getDescendants… methods of SwingUtils are especially useful for obtaining a reference to a contained component of a complex widget like JComboBox or JFileChooser. This line of code will return a reference to the arrow button of a JComboBox:

JButton button = SwingUtils.getDescendantsOfType(JButton.class, jComboBox).get(0);

This shows how to disable the text field of a JFileChooser to ensure the user can open only a listed file:

JFileChooser jFileChooser = new JFileChooser();
JTextField jTextField = SwingUtils.getDescendantOfType(
JTextField.class, jFileChooser, "Text", "");
jTextField.setEditable(false);
jFileChooser.showOpenDialog(null);

These methods can be equally useful for obtaining references to components in your own or third-party GUIs and may be a convenient alternative to providing accessors for components, especially when the component to be accessed will be identified by a runtime property.

The remaining methods are more in the nature of investigative tools to aid in designing and troubleshooting a Swing GUI.

Get The Code

SwingUtils.java

Related Reading

Java API: javax/swing/SwingUtilities
Java API: javax/swing/JFileChooser

2 Responses to “Swing Utils”

  1. Torgil Zethson said

    There are a couple of problems with SwingUtils:

    1. Looking for a method called “get[Property]” is too restrictive. What if I want to use the enabled property, the getter of which is called “isEnabled”, not “getEnabled”?

    A better way of handling this, IMO, would be to use property in the conventional Java Beans sense (Swing components are Java Beans, after all), and use the functionality in the java.beans package.

    2. The recursive search down into the component tree is always depth-first. This in itself may not be a problem, but it should at least be documented, especially for the methods that return a List or a “first” component. The order of the List, or which component comes “first”, depends on how the tree is traversed.

    For example, it is not obvious that the following code snippet will print “Nested Button”, even though “Root Button” actually appears one level above “Nested Button” in the hiearchy:

    JPanel root = new JPanel();
    JPanel nested = new JPanel();
    nested.add(new JButton(“Nested Button”));
    root.add(nested);
    root.add(new JButton(“Root Button”));
    JButton first = SwingUtils.getDescendantOfClass(JButton.class, root, “Icon”, null);
    System.out.println(first.getText());

  2. Darryl Burke said

    Torgil, thank you for the feedback.

    SwingUtils has been enhanced to cover boolean properties, and the depth-first nature of the component search routine has been mentioned in the doc comments.

    I’ll consider making over the class to use the java.beans classes, but right now I’m busy with some other stuff, as you will see in a day or two. Thanks for the suggestion though.

    Happy coding, Darryl

Leave a comment