Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

08 October 2010

Java JDK installation on Windows

Installing Java on Windows for development can be one of the trickier things, especially if you're looking for Java 1.5 vs. 1.6: Java 1.5 is not readily available for download. However, some digging reveals this great site with a link to the download (which requires registration). From there, you can find the 32- and 64-bit versions of the Windows' installers.

18 August 2009

Auto-generate Java getter/setter methods in Eclipse

The Eclipse IDE is a powerful tool for Java development. One feature I just discovered: How to automatically generate getter/setter methods (properties) for your classes.

In your class, place your cursor on a private member, and you'll notice a yellow underline and a little popup with options. One of them is "Create getter and setter for 'myPrivateVariable'". Simply click this choice and Eclipse displays a dialog to choose getter and setter method names as well as other options. You can also access this getter/setter auto-generate functionality by placing your cursor on a private field and then going to Refactor > Encapsulate Field.

06 June 2009

Populate and validate JSF selectOneMenu

Using a backing bean or Seam component (on JBoss), you can easily populate a JSF selectOneMenu (JSF dropdown box server-side control):

<h:selectonemenu id="lstRating" value="#{asset.rating}" required="true" requiredMessage="Rating is required.">
<f:selectitems value="#{asset.ratingItems}" />
</h:selectonemenu>
<rich:message for="lstRating" styleclass="errors" />

In this example, you have a selectOneMenu with id of lstRating. It gets its SelectItem elements from the asset.ratingItems property on the backing component. The selected item is stored in asset.rating.

  private String rating;
private ArrayList<SelectItem> ratingItems = null;

public ArrayList<SelectItem> getRatingItems(){
ratingItems = new ArrayList<SelectItem>();
ratingItems.add(new SelectItem(null,"Select One..."));
ratingItems.add(new SelectItem("TV-Y", "TV-Y"));
ratingItems.add(new SelectItem("TV-Y7", "TV-Y7"));
ratingItems.add(new SelectItem("TV-G", "TV-G"));
ratingItems.add(new SelectItem("TV-PG", "TV-PG"));
ratingItems.add(new SelectItem("TV-14", "TV-14"));
ratingItems.add(new SelectItem("TV-MA", "TV-MA"));

return ratingItems;
}

public String getRating(){
return this.rating;
}

public void setRating(String rating){
this.rating = rating;
}

In the above code, we declare an ArrayList of SelectItem object type. The getRatingItems() property instantiates the ratingItems ArrayList and populates it with SelectItem objects. Note the first SelectItem has a null value; if the user leaves the dropdown box to this item, the required validator will detect it as a blank value and throw a validation error.

23 January 2009

Create a zip archive in Java

Here's a Java class to zip up files in a directory. Thanks to the Java forum for their help in fixing some bugs.

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.io.File;

public class Zipper
{
private String _zipFilePath = "";
private String _imageFolderPath = "";
private String _zipFileName = "";
private static final int BUFFER = 20480;
private String _error = "";

public Zipper()
{
}

public void setZipFilePath(String path)
{
_zipFilePath = path;
}

public void setImageFolderPath(String path)
{
_imageFolderPath = path;
}

public String getError()
{
return _error;
}

public void zipEm()
{
File directory = new File(_imageFolderPath);

File files[] = directory.listFiles();

try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(_zipFilePath);
ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(dest));

byte data[] = new byte[BUFFER];

File f = null;
ZipEntry entry = null;
FileInputStream fi = null;
int count;

// Iterate over the file list and add them to the zip file.
for (int i=0; i <files.length; i++)
{
fi = new FileInputStream(files[i]);
origin = new BufferedInputStream(fi, BUFFER);
entry = new ZipEntry(files[i].getName());
zip.putNextEntry(entry);

while((count = origin.read(data,0,BUFFER)) != -1)
{
zip.write(data,0,count);
}
count = 0;
origin.close();
}
zip.close();
}
catch (Exception e)
{
// handle exception
_error = e.getMessage();
}
}
}

15 January 2009

Powerful image editing in Java

The Java Advanced Imaging (JAI) API provides a very powerful way of manipulating images on the server. For a current app under development, we needed to receive an image file (JPG, PNG, or GIF) and resize it to a set width (letting the height adjust to maintain aspect ratio). JAI came to the rescue when the built-in image processing packages in Java were too slow.

The following code demonstrates the image resizing capabilities of JAI; for maximum image quality, we use the "SubsampleAverage". Thanks to this article.

BufferedImage resizedImage = null;

try {
SeekableStream stream = SeekableStream.wrapInputStream(is, true);
RenderedOp newImage = JAI.create("stream",stream);
((OpImage)newImage.getRendering()).setTileCache(null);

double scale = targetWidth / newImage.getWidth();

ParameterBlock pb = new ParameterBlock();
pb.addSource(newImage);
pb.add(scale);
pb.add(scale);

/*
* For best results, must use the following rendering hints.
*/
RenderingHints qualityHints =
new RenderingHints(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);

resizedImage = (JAI.create("SubsampleAverage",
pb,qualityHints)).getAsBufferedImage();
}
catch (Exception e)
{
this.error = e.getMessage();
}

09 January 2009

Extracting contents of *.jar file

A quick and painless method: Rename the file's extension to .zip and double-click it in Windows Explorer. We're using Java 1.4.2_12, which doesn't have the jar.exe built-in. Otherwise, if your path environment variable is set correctly, you can just type jar xf jar-file at the command line to extract contents.

04 November 2008

Java String Comparison

One little gotcha in Java is the string comparisons: Using the equality check == will tell you if the two variables are referencing the same object, not whether their string values are the same. To do a string comparison, use something such as this,

 if (string1.equalsIgnoreCase(string2)) {
//... do something
}

Otherwise you'll be wondering why your code isn't working ;)