Sunday, October 16, 2011

How to create a Closable Tabbed pane in java

Preamble

This article shows how to create a closable tabbed pane in java

JDK and eclipse compatibility 

JDK version 1.3 with eclipse 3.3 is the minimum requirement.

Project overview 

The downloadable zip file contains an eclipse project which has a MainFrame class for GUI. Clicking on the button will create a new tab in the tabbed pane. To close press the tab close button.

Unzip the downloadable and load in the eclipse project. Make a run configuration for MainFrame class and you are all set.

final CloseableTabbedPane tabbedPane = new CloseableTabbedPane();
frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
//to add a new tab

tabbedPane.addTab("New tab", new JComponent()); 


How to preview image in JFileChooser

Preamble 

This article shows how to add image preview to a JFileChooser.

JDK and eclipse compatibility 

JDK version 1.3 with eclipse 3.3 is the minimum requirement.

Project overview 

The downloadable zip file contains an eclipse project which has a MainFrame class for GUI. Clicking on the button will launch the file chooser. Selecting an image will display a preview in the right pane.

Unzip the downloadable and load in the eclipse project. Make a run configuration for MainFrame class and you are all set.

Code usage

//attach image preview to FileChooser
ImagePreview preview = new ImagePreview(chooser);
chooser.setAccessory(preview);

Enjoy, if you like it please appreciate!

Java ZipInputStream with IBM PC or MS-DOS code page 437 (CP437) support

Preamble

This articles will show how to leverage the existing java ZipInputStream to also support IBM PC or MS-DOS code page 437

JDK and eclipse compatibility

JDK version 1.4 with eclipse 3.3 is the minimum requirement.

Project overview

The downloadable zip file contains an eclipse project which has a MainClass for displaying the sample usage.

The magic lies in ExtendedZipInputStream class which supports all the features provided by java ZipInputStream in addition to Extended ASCII file names support.

Unzip the downloadable and load in the eclipse project. Make a run configuration for MainClass and you are all set. The magic resides in the "getString" method.

/**
     * Read string with UTF-8 if fails use CP437 for the specified byte array
     * 
     * @param b The byte array to read the string from
     * @param off The offset to start reading from
     * @param len The total length to read
     * @return Either UTF-8 or CP437 string
     */
    private String getString(byte[] b, int off, int len) {
        String name;

        try {
            if (this.charset == null) name = (String) Reflect.invoke(this, DEF_READ_UTF8_METHOD, b,
                off, len);
            else name = new String(b, off, len, this.charset);
        }
        catch (Exception e) {
            //Unable to determine UTF-8 file name so use CP437
            name = this.getCP437String(b, off, len);
        }
        return name;
    }
Enjoy, if you like it please appreciate!

How to create an oval icon in java

package com.stark;

import java.awt.Component;
import java.awt.Graphics;

import javax.swing.Icon;

/***
 * A simple icon implementation that draws ovals.
 * 
 * @author Noman Naeem
 * 
 */
public class OvalIcon implements Icon {

    private int width, height;

    public OvalIcon(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.drawOval(x, y, this.width - 1, this.height - 1);
    }

    public int getIconWidth() {
        return this.width;
    }

    public int getIconHeight() {
        return this.height;
    }
}