Java Swing

Ken Webb 2010-06-09T10:52:19Z

Swing is the widget framework that ships with the standard edition of Java. This framework includes many different component types, instances of which are combined hierarchically to create GUIs.

The Sun Swing tutorial states that "to appear onscreen, every GUI component must be part of a containment hierarchy. A containment hierarchy is a tree of components that has a top-level container as its root. ... This top-level container is the root of a containment hierarchy — the hierarchy that contains all of the Swing components that appear inside the top-level container."

With a Xholon application, Swing GUIs can optionally be defined declaratively in one or more XML files, instead of programmatically as lines of code in a Java program. Swing components are treated just like any other objects in a Xholon containment hierarchy.

The following diagram shows part of the hierarchical structure of a sample GUI. The window on the left is the default Xholon GUI that is optionally available with every Xholon application. It can be used to view the entire containment hierarchy of the Xholon app.

The other panels in the diagram are parts of the sample GUI that has been specified declaratively in a set of XML files. The best way to understand what's happening here is to compare the contents of the tree on the left, with the appearance of the GUI under test. For example, node ButtonC:jButton_203 in the tree, is one of the buttons labeled BUTTON C in the GUI.

The following XML specifies the small window with the title "A small JFrame". The original text for the JButton was "Mars", but it now reads "123.0" after having been updated at runtime to indicate that the timestep is 123. In the tree view on the left of the diagram, this JFrame is the node labeled jFrame_150, with its child jPanel_151, and its grandchild jButton_152.

<!-- A small JFrame -->
  <Attribute_String roleName="Title">A small JFrame</Attribute_String>
    <Attribute_String roleName="PreferredSize">Dimension,200,200</Attribute_String>
    <Attribute_String roleName="Background">PINK</Attribute_String>
      <Attribute_String roleName="Text">Mars</Attribute_String>
      <Attribute_String roleName="ToolTipText">This is a Martian button.</Attribute_String>
      <!--<Attribute_String roleName="ActionCommand">onToMars</Attribute_String>-->

Alternatively, the above JFrame can be declared more concisely as:

<JFrame Title="A small JFrame">
  <JPanel PreferredSize="Dimension,200,200" Background="ORANGE">
    <JButton Text="Mars" ToolTipText="This is a Martian button." ActionCommand="onToMars"/>

If you run a Swing application with a Java debugger, you can inspect Swing's own internal runtime hierarchical structure. The following diagram shows a small part of that nested structure using the Eclipse Java debugger. Instances of Component are nested inside of other instances of Component, and each such component is specialized as a JPanel, JLabel, or whatever. So each component in a Swing application has zero or more child components, and also has a parent (not shown) referencing its container component. If it's the root JFrame component, then it doesn't have a parent. With the dubugger it's hard to see very much of this basic structure, because Swing components each have a large number of other attributes.

return to main page