Monday, December 28, 2009

Add Shutdown Icon to Desktop

Today, I added a shutdown icon to my Dad's Windows XP desktop, to make it easier for him to turn off the computer. This is how you can do it:
  • Right-click on the desktop and choose New > Shortcut.
  • Type shutdown.exe -s -t 00 in the location box and press Next.
  • Type Turn off computer as the name of the shortcut and then press Finish.
  • Right-click the new icon that has appeared on the desktop and select Properties.
  • Click the Change Icon... button on the Shortcut tab.
  • Type %SystemRoot%\system32\SHELL32.dll in the location box and pick the Shutdown icon, which looks like a red square containing a circle with a vertical line.
Now, you can shutdown your computer by simply clicking the "Turn off computer" icon on your desktop, instead of going via the Start menu.

Adding a JProgressBar to a JTable Cell

Create a cell TableCellRenderer which uses a JProgressBar as follows:
import java.awt.Component;

import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

public class ProgressCellRenderer extends JProgressBar
                        implements TableCellRenderer {

   * Creates a JProgressBar with the range 0,100.
  public ProgressCellRenderer(){
    super(0, 100);

  public Component getTableCellRendererComponent(
                                    JTable table,
                                    Object value,
                                    boolean isSelected,
                                    boolean hasFocus,
                                    int row,
                                    int column) {

    //value is a percentage e.g. 95%
    final String sValue = value.toString();
    int index = sValue.indexOf('%');
    if (index != -1) {
      int p = 0;
        p = Integer.parseInt(sValue.substring(0, index));
      catch(NumberFormatException e){
    return this;
Apply the cell renderer to a specific column in the table:
JTable myTable = new JTable();
TableColumn myCol = myTable.getColumnModel().getColumn(1);
myCol.setCellRenderer(new ProgressCellRenderer());
Now whenever you update a value in that column, the JProgressBar will get updated accordingly.

Sunday, December 27, 2009

Using Hermes to Browse WebLogic Topics/Queues

Hermes is a useful tool which allows you to browse JMS topics and queues. I use WebLogic as my JMS provider and it was not trivial trying to connect to my topic using Hermes, so I thought I'd post instructions to help others trying to do the same.

Here are the steps you need to take, in order to browse WebLogic queues and topics using Hermes:

1. Install HermesJMS

  • Download Hermes from here.
  • It comes as an installer jar file which you can run using the command: java -jar hermes-installer-1.13.jar.
  • Once installed, start it using hermes.bat.

2. Add WebLogic Provider
You need to add the weblogic jar to the classpath as follows:

  • On the menubar go to Actions > New > New session... to open the Preferences dialog.
  • Click on the Providers tab.
  • Right-click to Add Group and call it "weblogic92", for example.
  • Right-click on Library to Add JAR(s) and select your weblogic jar file from the file chooser dialog.
  • Select Don't Scan when prompted to scan the jar file.
  • Click Apply and close the dialog.
3. Create WebLogic Session
  • On the menubar go to Actions > New > New session... to open the Preferences dialog.
  • Click on the Sessions tab.
  • Type a name for the session. For example, "myweblogic".
  • In the Plug In list select BEA WebLogic.
  • Under Connection Factory class, pick hermes.JNDITopicConnectionFactory.
  • Select weblogic92 (defined in step 2) from the Loader dropdown.
  • Add property: initialContextFactory with a value of weblogic.jndi.WLInitialContextFactory.
  • Add property: providerURL with a value of your URL e.g. t3://myhost:2120.
  • Add property: binding with a value of the name of your connection factory e.g. myConnectionFactory.
  • Add any other properties you may have e.g. securityCredentials etc.
  • Remove all Destinations by right-clicking and selecting Remove.
  • Press OK.
4. Discover Topics/Queues
  • On the left navigation tree, go into jms > sessions > myweblogic.
  • Right-click "myweblogic" (the new session created in step 3), and click Discover. You will see a list of queues and topics appear.
  • Click on any one of them to browse.

Friday, December 25, 2009

Generics and Class.forName

This post shows how you can create objects of a specified class, using a class name and the supertype of the class you are trying to create.
public final class BeanCreator {

   * Suppress constructor.
  private BeanCreator(){

   * Creates an object of the class name and supertype.
   * @param <T>
   * @param className
   * @param superType
   * @return
   * @throws ClassNotFoundException
  public static <T> T create(final String className,
             final Class<T> superType) throws Exception {
    final Class< ? extends T> clazz =
    return create(clazz);

   * Creates an object of the specified class using
   * its public or private no-arg constructor.
   * @param <T>
   * @param classToCreate
   * @return
  public static <T> T create(final Class<T> classToCreate)
                                      throws Exception {
        final Constructor<T> constructor =
        if (constructor == null) {
         throw new Exception("Could not create a new "+
         "instance of the dest object: " + classToCreate
         + ".  Could not find a no-arg constructor.");

        // If private, make it accessible
        if (!constructor.isAccessible()) {
        return constructor.newInstance();