Wednesday, 19 August 2015

Everything you need to know about z-index

Often, you encounter situations in which you don't like the natural stacking order of elements rendered by your browser. For example, you want the tool tips to come above the element on which it is hovered, or the dialog boxes to come over all the components of the page. In comes the z-index property of CSS.

z-index, as the name suggests, is used to align items on the z-axis; the axis perpendicular to screen which you can imagine coming towards you. The value of z-index can be positive, zero or negative integer, and the higher the value, the closer it will be to you. It is worthy to note that z-index works only when the element is given a position, i.e. absolute, relative or fixed.

Before getting into more details, lets first understand how the elements are stacked in HTML. The order given is from back to front, and to my best understanding of the much complicated, but detailed W3.org website. 
  1. First comes positioned elements which have negative z-index.
  2. Elements which have no position, no inline and no float attributes comes next in the picture.
  3. The elements which have been given no position attribute but have a floating attribute.
  4. The elements which have been given no position attribute but have an inline attribute.
  5. The positioned elements comes at last. Elements with higher z-index values are stacked closer to you at the top.
The default z-index value of an element is called auto. auto means that the element will take the z-index of its parent. While it does not find any z-index value, it will recurse all the way to the top, where the <html> node has a z-index of 0. Also, whenever there is a tie between elements in the above ordering, the one which comes before in the HTML dom, are stacked first, so they are farther away from you.


Examples:

Enough of theory, now lets take a look at some examples, which should clear all doubts. In these examples, I have created two divs (boxes) of different colors and borders, so that they can be recognized easily. I have given a negative top margin to the second div, so that it can overlap with the first one, otherwise z-index would never come into picture. Occasionally I have created additional divs like a child div or a floating div to enhance your concepts.
  1. Two Div without z-index[Link to fiddle]: None of the elements are given z-index. The first to come is stacked first, so the red one is farther away from you.
  2. Two Div with z-index[Link to fiddle]: Both elements are given a position of relative. Additionally a z-index of 1 is given to the red box. Hence it goes above the green box.
  3. Introducing a child div[Link to fiddle]: I introduced a child div to the first div in the above case and did not give it any z-index. Which means it takes a z-index of auto and looks for a z-index from it parent. It gets a value of 1 and so it is stacked above that of green box.
  4. Float and inline divs[Link to fiddle]: This is a much more complicated case. I created 2 more div one with float(yellow) and one with inline display(brown). At the bottom is green box. It satisfies case 2 of the above ordering, and has no inline, float and no position attributes. Next we can see yellow. It satisfies case 3 and is floating type. Next comes brown, it satisfies case 4 and is inline. Finally we have a positioned element red, with z-index = 1 which sits atop everyone.

I hope these examples cleared your doubts regarding z-index and you enjoyed reading and learning from it.  

Sunday, 16 August 2015

Base64 Image in a Jrxml file

Sometimes we are dealing with Base 64 encoded images and let's say we need to include it in our jrxml file for exporting to pdf. I encountered a similar problem today, and while it seems easy at first, it can be very intruding and frustrating if you are unable to find the right moves. Here are the steps I used to solve the problem.


  1. Create an image tag <image>.
  2. Inside the <image> tag, create a <reportElement> tag. This tag can be used to add attributes to the the image like its position and size.
    Example :
    <reportElement x="437" y="85" width="92" height="108" uuid="7330c3ba-a8ed-4bfa-bbe6-a008c9e6558c"/>
  3. Finally, add an <imageExpression> tag as a sibling to the <reportElement> tag. The data inside the <imageExpression> tag is where you put your base64 image, and here is where you need to be careful. Otherwise you will keep beating the bush without finding the image on your pdf.
    • Put the base 64 string inside the CDATA block.
    • Remember to enclose the string withing quotes.
    • Do not put the html image renderer data:image/jpeg;base64,as a prefix to your base64 string.
    • Enclose the data within new java.io.ByteArrayInputStream(new sun.misc.BASE64Decoder().decodeBuffer())for decoding the base64 string.
An example: 


<image onErrorType="Icon">
  <reportElement x="437" y="85" width="92" height="108" uuid="7330c3.."/>
  <imageExpression>
<![CDATA[new java.io.ByteArrayInputStream(new sun.misc.BASE64Decoder().decodeBuffer(
"/9j/4AAQSkZJRgAB ......"))]]></imageExpression>
</image> 

Thursday, 13 August 2015

Hashtable Vs HashMap in Java

HashMap and Hashtable are the hashing/mapping libraries of java that I have used. However, I never went into details such as what are their implementations, why there are two of them with the same purpose, or if they are different, when should I use them?
  1. HashMap allows null values to be stored in the key or values. Hashtable doesn't. So if you are not absolutely sure if your keys or values will be non-null go for HashMap to avoid Runtime Errors.
  2. HashMap is not synchronized whereas Hashtable are. So, If your application is multi-threaded and these insert or remove keys from the map then you should use Hashtable. Lest, one of the threads modifies the map and the other thread will get inconsistent data.
  3. Because of the extra thread safety and synchronization issues that Hashtable handles, they are slower than HashMap. I tested get and put methods of both of them for 1 million inserts and access over 10 attempts of random data. Average time for insertion in HashMap was 309ms while that of Hashtable was 1177ms. Whereas average time for retrieval in HashMap was 87ms while that of Hashtable was 143ms. 
  4. HashMap uses Iterator class for iterating whereas Hashtable uses Enumerator. This means that if the HashMap is structurally modified after creating the Iterator(i.e. some element is added or removed from the map), then a ConcurrentModificationException will be thrown. Hashtable is fail-safe from this.
To summarize, we can use HashMap whenever we are not bothered about multi-threading or other concurrent modifications on the map. It will allow nulls to be part of the map and also give faster performance. If multiple threads concurrently modify or if we are strict about nulls not being a part of the map then Hashtable should be used.