[ Prev ] [ Index ] [ Next ]

Java JDK/JRE

Created Friday 22/08/2006

1. Certificate management

Certificate management in the JDK is done with keytool, a generic certificate store management tool provided with the Java SDK. The keytool is used to create, list and otherwise manage the contents of PKCS#12, PKCS#7 and JKS certificate stores.

2. Packaging as installer

IzPack is a java packager that provides the installer as a .jar file with manifest that can be executed with "java -jar x.jar" and provides the standard type wizard installer "next, back, finish" and lets the user choose where they want to install to and the like.

3. Manifest

The JDK Manifest reader terminates when it finds a blank line, therefore be sure that manifests don't have sections and are all jamed up one line following the other.

4. Current stack point

On linux platforms ctrl+\ at the shell that started the jvm will show the current stack trace. On windows, typing ctrl+break prints the stack trace.

5. Layout Managers

Widget layout in swing is done with a Layout Manager (since the JDK 1.2, swing provides LayoutManger2 - describe difference). The JDK has heaps of layout managers, of which BorderLayout, SpringLayout and GridBagLayout are the most useful. Of these, the simplest by far is the BorderLayout.

5.A. BorderLayout

The BorderLayout is easy to use and provides automatic relative resizing of it's components. The layout is divided into areas NORTH, SOUTH, EAST, WEST and CENTER, with CENTER getting most of the resize space by default. The BorderLayout provoides constants to refer to these layout regions:

The BorderLayout can also be used to provide a stack-like layout with components appearing at the top and bottom. Use the constants BorderLayout.PAGE_START, BorderLayout.PAGE_END

Example 5.A.1: Layout 3 components in a row

JPanel p = new JPanel(new BorderLayout());
p.add(new JLabel("x"), BorderLayout.WEST);
p.add(new JLabel("y"), BorderLayout.CENTER); // gets resize space
p.add(new JLabel("z"), BorderLayout.EAST);

Example 5.A.2: Layout 2 components in a stack

JPanel p = new JPanel(new BorderLayout());
p.add(new JLabel("x"), BorderLayout.PAGE_START);
p.add(new JLabel("y"), BorderLayout.PAGE_END);

7. Look and Feel

Tip: The Sun documentation has extensive information on setting The Platform LAF

Java supports a dynamically changable Look and Feel (LAF). The LAF can be specified in a number of ways, either on the command line or by programmaticly setting the LAF on the UIManager. The UIManager is the object responsible for setting the LAF and consequently provides the setLookAndFeel behaviour to allow client-gui code to perform this task.

Note: Programatically setting the LAF must be done in a try/catch (typically for Throwable)

7.A Command line LAF

Setting the LAF on the command line is achieved by setting the JDK Property swing.defaultlaf, which must be set to the fully qualified name of the Look and Feel class. For example, to specify the GTK Look and Feel, set the defaultlaf property as follows:

-Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel

7.B Setting the LAF via the UIManager

Note: If the look and feel is a 3rd party LAF, be sure to register the LAF with the UIManager, see #7.C

Tip: When invoking setLookAndFeel it's easier to catch Throwable rather than catching ClassNotFoundException, InstantiationException, IllegalAccessException and UnsupportedLookAndFeelException

The UIManager is responsible for supporting global UI resource and component configuration. This includes the choice of Look and Feel that should be applied to all frames, components and other UI resources. The UIManager provides access to setting the LAF through the method setLookAndFeel(String), which takes the fully qualified LAF class name as the argument. The UIManager throws an UnsupportedLookAndFeelException if the LAF given in the argument is either non-existent or is not a supported LAF (i.e., does not extend BasicLookAndFeel).

Typically, setting the LAF should be done very early on in the execution path. This might be in the main(String[]) or in a method invoked by main. Either way, to ensure a consistent behaviour, be sure that the LAF is set prior to opening any frame or ui component. As an example, to set the LAF to the GTK Look And Feel, invoke the UIManager's setLookAndFeel in a try/catch block as follows:

try {
    UIManager.setLookAndFeel(
        UIManager.setLookAndFeel(com.sun.java.swing.plaf.gtk.GTKLookAndFeel.class.getName());
} catch (Throwable t) {
    Logger.getLogger(this).warn("Failed to set the look and feel", t);
}

7.C Installing a 3rd party LAF

Tip: Catching Throwable is perfectly reasonable when intalling and setting the LAF on the UIManager

There are many LAFs created for java, some of them quite stunning (see for example http://skinlaf.com). In addition to these LAF's being available to the JVM (e.g., in a JAR which is in the CLASSPATH), these 3rd party LAFs must be installed, so that relevent properties are and resources are made available to the JVM under which the LAF is running. Much like setting a particular LAF, the UIManager is also responsible for LAF installation, which must be done prior to setting the LAF. The UIManager provides installLookAndFeel(String,String), which adds the named look and feel into the UIManager's LAF dictionary, under the key specified in the first argument).

For example, to use SkinLF as the LAF, first be sure that the both skinlf.jar and laf-plugin.jar are made available to the JVM via the CLASSPATH and then invoke 'installLookAndFeel and setLookAndFeel on the UIManager as follows:

try {
     UIManager.installLookAndFeel("SkinLF", "com.l2fprod.gui.plaf.skin.SkinLookAndFeel");
     UIManager.setLookAndFeel(com.l2fprod.gui.plaf.skin.SkinLookAndFeel.class.getName());
} catch (Throwable t) {
    Logger.getLogger(this).warn("Failed to install and set LAF", t);
}

7.D Using ''swing.properties'' to set the LAF

Note: The swing.properties are located in the JRE lib directory

The swing.properties file is another way to specify the choice of LAF. Essentially, this approach is similar to 7.A in that the swing.properties file is used to specify the LAF by setting the swing.defaultlaf. The swing.properties is an optional file (so it may not exist), which is located in JRE lib directory (not the JDK lib directory). Following the same example as for #7.A, it is possible to set the LAF to the GTKLookAndFeel by addong the following entry to the swing.properties:

bash $ cd $JAVA_HOME/jre/lib
bash $ cat swing.properties
# Swing properties
swing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel

7.E Dynamically changing the LAF after startup

Note: Information in this section is adapted from the Sun Platform LAF documentation.

It is possible to change the LAF with setLookAndFeel even after frame or UI Components are made visible. To ensure that existing components have the new LAF applied, be sure to invoke the updateComponentTreeUI (from SwingUtilities), for each top-level container. It may be necessary to ''''resize each of the top-level containers, so that components within the container have the correct size for the new LAF. E.g.:

// assume this is in a try/catch block...
UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();

7.F How the UIManager performs LAF selection

Note: The UIManager selects a LAF when setLookAndFeel is invoked or when the default LAF is is (on startup)

The algorithm used by the UIManager in selecting a LAF, which is done either when setLookAndFeel is invoked or when the default LAF is applied on startup, is as follows:

Note: This information is adapted from Sun Platform LAF (section: steps)

7.G LAF Example

Sun provide a LookAndFeelDemo. Other examples and codelets can be found Platform LAF (section:examples). Be sure also to have a look at PLAF Themes, Cross-platform Look and Feels and the SwingSet Demo V2. As a final note, see also the Sun Platform LAF Documentation

8. DOM Trees

A DOM Tree reads the entire XML structure into memory. Clearly, tjhere is a trade off in terms of memory space vs. ease of use in accessing the entire node structure (compare to SAX, which is event driven). If the node structure is sufficiently large, then attempting to create a DOM tree may result in out of memory errors.

8.A Creating a DOM Tree with w3c DOM

The standard DOM Tree provided in java produces a DOM Tree (w3c Document) containing (w3c) Nodes. This type of Document is not compatable with a JDOM tree

8.A Creating a DOM Tree with DOM4J

JDOM provides easier tree operations than the standard w3c Documents. Nodes can be added trivially simply by providing the fully qaulified string path to the new node (e.g., "/doc/some/node"). The following example creates a DOM tree from a pathname:

import org.dom4j.*;
import org.dom4j.io.*;

// Create a dom4j document from file
Document doc1 = new SAXReader().read(xmlFile);
Node node1 = document.selectSingleNode("/doc/some/node");

// Or to create a DOM tree from an xml string:
Document doc2 = DocumentHelper.parseText("<doc><some></node></some></doc>");
Node node2 = document.selectSingleNode("/doc/some/node");

Stuart Moorfoot © 13 June 2003 foo@bund.com.au


Backlinks: :bsh :xslt :yum