Change to a Mac Look and Feel on a Windows Machine

There are two ways to implement Mac L&F on Windows. The first consists of modifying the main L&F class to return true when the isSupportedLookAndFeel is called. This is against the Swing/JDK license agreement, though.

Another way is to use a 3rd party Mac look & feel. Check out the website in the related URLs section for more information. Remember that you can dynamically load a look and feel without having to modify a properties file.

Turning off DTD validation

As soon as you create the DocumentBuilderFactory, call the method setValidating and set it to false.

Try out following example and set it to true to see the parsing errors.

customers.dtd (!!remove the space between ? and xml):

<? xml version="1.0" encoding="UTF-8"?>
<!ELEMENT customer (name, addresses+, email)+ >
<!ATTLIST customer custid CDATA #REQUIRED>
 
<!ELEMENT name (#PCDATA)>
 
<!ELEMENT addresses (addline1, addline2, zip, location, state)>
<!ELEMENT addline1 (#PCDATA)>
<!ELEMENT addline2 (#PCDATA)>
<!ELEMENT zip (#PCDATA)>
<!ELEMENT location (#PCDATA)>
<!ELEMENT state (#PCDATA)>
 
<!ELEMENT email (#PCDATA)>

customers.xml (!!remove the space between ? and xml):

<? xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE customers SYSTEM "customers.dtd">
<customers>
   <customer id="C12345" type="prio1">
      <name>Joris Van den Bogaert</name>
      <address>
         <addressline>Handelskaai 3</addressline>
         <zip>1000</zip>
         <location>Brussels</location>
         <country>BELGIUM</country>
      </address>
   </customer>
</customers>

Main.java:

import org.w3c.dom.*;
  
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
 
import java.io.*;
   
public class Main
{
   public static void main(String []args) {
      Document doc;
  
      try {
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         dbf.setValidating(true);
         DocumentBuilder db = dbf.newDocumentBuilder();
         doc = db.parse(new File("customers.xml"));
      }
      catch(Exception e) {
         e.printStackTrace();
      }
   } 
}

List the children of a directory using the JNDI file system provider

This example lists the files in the c:\temp directory.

Main.java:

import javax.naming.*;
import java.util.*;
import java.io.*;
 
public class Main
{
   public static void main(String []args) {
      try {
         Properties properties = new Properties();
         properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
         properties.put(Context.PROVIDER_URL, "file:///");
 
         Context ctx = new InitialContext(properties);
 
         // List the objects 
         NamingEnumeration enum = ctx.list("c:\temp");
         while (enum.hasMore()) {
            NameClassPair ncPair = (NameClassPair) enum.next();
            System.out.println(ncPair.getName() + "t" + ncPair.getClassName());
         }
         ctx.close();
 
      }
      catch(NamingException ne) {
         ne.printStackTrace();
      }
   }
}

Notice that every element that the method list returns is of type NameClassPair. If you actually want to do an operation on a child, you would have to get it with lookup.

If you want to perform an operation on all the children, you can use the alternative method listBindings which, instead of NameClassPair, returns Binding instances. Binding is a subclass of NameClassPair that also contains the object itself (accessible with getObject).

Main.java:

import javax.naming.*;
import java.util.*;
import java.io.*;
 
public class Main
{
   public static void main(String []args) {
      try {
         Properties properties = new Properties();
         properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
         properties.put(Context.PROVIDER_URL, "file:///");
 
         Context ctx = new InitialContext(properties);
 
         // List the objects 
         NamingEnumeration enum = ctx.listBindings("c:\temp");
         while (enum.hasMore()) {
            Binding binding = (Binding) enum.next();
            System.out.println(binding.getName() + "t" + binding.getClassName());
            System.out.println(binding.getObject());
         }
         ctx.close();
 
      }
      catch(NamingException ne) {
         ne.printStackTrace();
      }
   }
}

Programmatically get information about your database driver

Main.java:

import java.util.*;
import java.sql.*;
  
public class Main {
   public static void main(String []args) throws Exception {
      try {
         Class.forName("org.gjt.mm.mysql.Driver").newInstance();
         Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.0.1/esus", 
                                                       "joris",
                                                       "mypass");
         DatabaseMetaData dbMeta = conn.getMetaData(); 
 
         System.out.println ("Database Product Name : " +
                                 dbMeta.getDatabaseProductName());
         System.out.println ("Database Product Version: " +
                                 dbMeta.getDatabaseProductVersion());
         System.out.println ("Database Driver Name : " +
                                 dbMeta.getDriverName());
         System.out.println ("Database Driver Version : " +
                                 dbMeta.getDriverVersion());
      }
      catch(Exception e) {
         e.printStackTrace();
      }
   } 
}

outputs:

Database Product Name : MySQL
Database Product Version: 3.23.42
Database Driver Name : Mark Matthews' MySQL Driver
Database Driver Version : 2.0.4

Using a shutdown hook

To gain control over when you application shuts down (eg. to close database connections), you can use the 1.3 method addShutdownHook in the Runtime class and pass it a thread object. The JVM will execute the thread when the application is about the end.

eg.

public class Main
{
   public static void main(String args[]) {

      Runtime.getRuntime().addShutdownHook(new Thread() {
         public void run() {
            System.out.println("Shutting down...");
         }
      });

      System.out.println("press ctrl-c to exit!");
      while (true) ;
   }
}

Native byte order on your machine

Simple, in JDK1.4+, use java.nio.ByteOrder.nativeOrder()

Main.java:

import java.nio.*;
 
public class Main
{
   public static void main(String []args) {
      ByteOrder bo = ByteOrder.nativeOrder();
      
      System.out.println("Native byte order of your platform is " + bo);
   }
}

outputs on my win machine:

Native byte order of your platform is LITTLE_ENDIAN

Showing the trademark symbol in a JLabel

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      final JLabel label = new JLabel("Javau2122");
      getContentPane().add(label);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      pack();
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}

Creating a label with SWT

Main.java:

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.*;
 
public class Main {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);

      // pos x, pos y, width, height
      shell.setBounds(200, 200, 300, 200);
      shell.setText("Label Demonstration");
      shell.setLayout(new GridLayout());

      Label label = new Label(shell, SWT.CENTER);
      label.setText("This is a label");

      shell.open();

      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
}

Using a drop down combobox in SWT

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
 
public class Main {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
 
      // pos x, pos y, width, height
      shell.setBounds(200, 200, 300, 200);
      shell.setText("SWT Combobox Demonstration");
      shell.setLayout(new GridLayout());
 
      String[] comboData = { "one", "two", "three", "four", "five",
                             "six", "seven", "eight", "nine", "ten",
                             "eleven", "twelve", "thirtheen", "fourteen",
                             "fifteen", "sixteen", "seventeen", "eighteen",
                             "nineteen", "twinty" };
 
      Group comboGroup = new Group(shell, SWT.NULL);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 1;
      comboGroup.setLayout(gridLayout);
      comboGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
      comboGroup.setText("Editable Combo");
       
      Combo combo = new Combo(comboGroup, SWT.DROP_DOWN);
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            Combo combo = ((Combo) event.widget);
            System.out.print(combo.getText());
            System.out.println(" selected = " + combo.getSelection());
         };
      };
      combo.addSelectionListener(selectionListener);
      combo.setItems(comboData);
       
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
}

Creating an always-on-top SWT Shell

Use the style SWT.ON_TOP when you create the Shell.

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
 
public class Main { 
   static Display display;
   static Shell shell;
   static Color color;
    
   public static void main(String[] args) {
      display = new Display();
      shell = new Shell(display);
 
      // pos x, pos y, width, height
      shell.setBounds(200, 200, 400, 200);
      shell.setText("SWT Shell Demonstration");
      shell.setLayout(new GridLayout());
 
      final Group buttonGroup = new Group(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 3;
      buttonGroup.setLayout(gridLayout);
      buttonGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
      final Button createShellButton = new Button(buttonGroup, SWT.PUSH);
      createShellButton.setText("Open Shell");
 
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            final Shell childShell = new Shell(SWT.RESIZE | SWT.CLOSE | SWT.ON_TOP);
            Button closeButton = new Button(childShell, SWT.PUSH);
            closeButton.setBounds(10, 10, 100, 30);
            closeButton.setText("Close Shell");
            closeButton.addListener(SWT.Selection, new Listener() {
               public void handleEvent(Event event) {
                  childShell.dispose();
               }
            });
          
            childShell.setSize (300, 100);
            childShell.setText ("Title of Shell");
            childShell.open ();                  
         };
      };
       
      createShellButton.addSelectionListener(selectionListener);
       
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      if (color != null && !color.isDisposed()) {
         color.dispose();
      }
      display.dispose();
   }
}