All posts in 'Java'

Enabling tooltips on a JTree

12-Aug-08

Thinks i keep forgetting. Today: Enabling a JTree in J2SE to show different tooltips on his nodes: 1. Create a custom tree renderer like so: import java.awt.Component;   import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer;   public class TooltipTreeRenderer extends DefaultTreeCellRenderer { @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int […]

Read the complete article »

regex: URL thingy with username, password, host and port

07-Jul-08

Just in case i do keep forgetting that stuff, here’s a regex for decoding urls like ftp://user:somepass@somehost:someport in Java: final Hashtable<String, Integer> portMap = new Hashtable<String, Integer>(); portMap.put("ftp", 21); portMap.put("sftp", 22);   final Pattern urlPattern = Pattern.compile("(ftp|sftp)://(\\S+):(\\S+)@([\\S&&[^:]]+)(:(\\d+))?");   final Matcher m = urlPattern.matcher(url); if(!m.matches()) throw new RuntimeException("Invalid ftp url!");   final String protocol = m.group(1).toLowerCase(); […]

Read the complete article »

Hibernate and Inheritance Mapping

27-May-08

Hibernate supports multiple types of inheritance mapping, some of them (Table per class, Table per subclass) using a discriminator column to decide the class. The type of the discriminator can be one of string, character, integer, byte, short, boolean, yes_no, true_false In case you need to use any other than string or character, i.e. integer, […]

Read the complete article »

Division by zero

26-May-08

Just a quick reminder for myself: int a = 0/0; // Throws ArithmeticException double d1 = 0/0.0; // d1 is NaN double d2 = 1/0.0; // d2 is Infinity double d3 = -1/0.0; // d3 is -Infinityint a = 0/0; // Throws ArithmeticException double d1 = 0/0.0; // d1 is NaN double d2 = 1/0.0; […]

Read the complete article »

SCJP, finally!

06-May-08

More than a year ago i decided to do the Sun Certified Java Programmer. Shortly after i bought this book, the projects at work and at home were somewhat overwhelming and after all, i realized that a big part of the SCJP is about some weird, crazy and sometimes wrong design decisions of the Java […]

Read the complete article »