JDev

Code, javaJune 29, 2007 4:16 pm

The String.replaceAll(String regex, String replacement) method can be used to escape single quotes in Java.

The correct usage of this method is not quite intuitive. If you construct sql statements in Java, single quotes in a string must be preceded with a backslash(\). However, if you put a single backslash before the quote, the method would still return an unescaped quote.  I tried putting a double backslash with the same result. It turns out that the correct number of backslashes is four, two for the regex engine and two for the java compiler:

    str = str.replaceAll("’","\\\\’");

The solution was found on the JavaRanch forum

 

Code, javascriptJune 28, 2007 6:59 pm

Sean McManus wrote a nice routine to implement a delay in JavaScript code:

 

 function pausecomp(millis)
{
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
        while(curDate-date < millis);
}

 

Code, javascript, firefox 1:56 pm

"Permission denied to set property XULElement.selectedIndex"

This bug may occur in Firefox when trying to use select() or focus() on an input box in Firefox.

It is fixed by turning autocomplete off:

obj.setAttribute(’autocomplete’,'off’);

 

Code, mysqlJune 25, 2007 7:36 pm

To select a random set of records from mysql, use the following statement:

SELECT * FROM table_name ORDER BY RAND() limit 5;

A random set of 5 records should be returned by the query. 

Code, javascriptJune 20, 2007 3:54 pm

I use this function from http://sedition.com/perl/javascript.html 

function randomColor(){
   var rgbColor = ‘’;
   for ( i = 1; i < 4; i++ ) {
      rgbColor +=  Math.floor(Math.random() * (250+1));
      if ( i != 3 ) rgbColor += ‘,’;
   }
   //window.status = rgbColor;
   return"rgb("+rgbColor+")";
 }

 Usage example:

element.style.backgroundColor = randomColor(); 

Code, cssJune 11, 2007 6:18 pm

To remove a vertical scrollbar from a textarea, use the following style :

overflow:hidden 

cssJune 5, 2007 8:04 pm

Page elements can be hidden from view by setting either visibility:hidden or display:none.

If visibility:hidden is used, the item will still occupy space on the page, whereas in case of display:none, it won’t.

Code, java 12:49 pm

Add an empty border to JTextArea: 

    myTextArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

javaJune 4, 2007 1:06 pm

Use the setMnemonic(char) method to set a hot key associated with a button in Java Swing, e.g.:

jButton.setMnemonic(’a');