<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>HQ&#039;s Serenity &#187; HQ Library</title>
	<atom:link href="http://fauzilhaqqi.net/tag/hq-library/feed/" rel="self" type="application/rss+xml" />
	<link>http://fauzilhaqqi.net</link>
	<description>Muhammad Fauzil Haqqi&#039;s Personal Blog</description>
	<lastBuildDate>Sat, 21 Aug 2010 02:55:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Membatasi Jumlah Karakter pada JTextField</title>
		<link>http://fauzilhaqqi.net/2010/02/membatasi-jumlah-karakter-pada-jtextfield/</link>
		<comments>http://fauzilhaqqi.net/2010/02/membatasi-jumlah-karakter-pada-jtextfield/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 06:00:19 +0000</pubDate>
		<dc:creator>Haqqi</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[HQ Library]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[NetBeans]]></category>
		<category><![CDATA[Source Code]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.fauzilhaqqi.net/?p=153</guid>
		<description><![CDATA[Sekali-sekali posting tutorial Java pake Bahasa Indonesia
Kadangkala, dalam membuat suatu program yang mengharuskan user mengisi suatu textfield, kita perlu membatasi jumlah karakter yang diketikkan. Dalam Java, class JTextField biasa tidak didesain untuk secara otomatis melakukannya. Coba saja Anda ketikkan satu juta karakter di sana, pasti masih bisa terpampang di layar. Nah, coba bayangkan kalau yang [...]]]></description>
			<content:encoded><![CDATA[<p><span class="highlight-red"><b>Sekali-sekali posting tutorial Java pake Bahasa Indonesia</b></span></p>
<p><a href="http://www.fauzilhaqqi.net/wp-content/uploads/2010/01/05-javablog.png"><img class="alignleft size-full wp-image-55" style="margin: 8px;" title="Java Blog" src="http://www.fauzilhaqqi.net/wp-content/uploads/2010/01/05-javablog.png" alt="" width="75" height="140" /></a>Kadangkala, dalam membuat suatu program yang mengharuskan user mengisi suatu textfield, kita perlu membatasi jumlah karakter yang diketikkan. Dalam Java, class JTextField biasa tidak didesain untuk secara otomatis melakukannya. Coba saja Anda ketikkan satu juta karakter di sana, pasti masih bisa terpampang di layar. Nah, coba bayangkan kalau yang harus diinputkan adalah field kode barang, atau nomor telepon, atau apapun yang memiliki batas panjang. Tentu akan merepotkan kalau di program harus mengecek itu.</p>
<p>Untuk itu lebih mudah jika kita membatasi jumlah karakter yang bisa diinputkan dalam JTextField. Salah satu gunanya adalah saat field yang Anda perlukan itu harus disimpan dalam kolom database yang memiliki lebar karakter terbatas. Dalam HQLibrary, saya juga berencana membuat utility yang menangani hal simpel ini. Yah, meskipun ini sebenarnya hal sederhana, rasanya tetap perlu dimasukkan untuk mempermudah nantinya.<br />
<span id="more-153"></span><br />
Oke, langsung masuk ke class yang saya buat. Class pertama, seperti biasa adalah Utility. Kali ini Class yang saya buat saya beri nama <code>HQTextFieldUtil</code>, yang nantinya akan berisi semua utility yang berhubungan dengan textfield.</p>
<pre class="brush: java;">
/**
 * DO NOT REMOVE THIS LICENSE
 *
 * This source code is created by Muhammad Fauzil Haqqi.
 * You can use and modify this source code freely but
 * you are forbidden to change or remove this license.
 *
 * Nick  : Haqqi
 * YM    : xp_guitarist
 * Email : fauzil.haqqi@gmail.com
 * Blog  : http://www.fauzilhaqqi.net
 */
package hq.utility;

import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Haqqi
 */
public final class HQTextFieldUtil {
  // *******variables area****** //

  // *****constructors area***** //
  private HQTextFieldUtil() {
  }

  // ********methods area******* //
  public static void limitTextFieldChar(JTextField field, final int numChar) {
    if (numChar == 0) {
      field.setDocument(new PlainDocument());
      return;
    }

    PlainDocument pd = new PlainDocument() {

      private static final long serialVersionUID = 1L;
      private int limit = numChar;

      @Override
      public void insertString(int offs, String str, AttributeSet a)
          throws BadLocationException {
        if (str == null) {
          return;
        }
        if ((getLength() + str.length()) &lt;= limit) {
          super.insertString(offs, str, a);
        }
      }
    };
    field.setDocument(pd);
  }
}
</pre>
<p>Pada method <code>limitTextFieldChar()</code>, akan dilakukan pembuatan suatu dokumen. Bagi yang belum mengerti, pada JTextField, sebenarnya kita tidak mengetikkan atau menyimpan hasil ketikan langsung di class milik swing tersebut. Alih-alih demikian, karakter-karakter yang kita ketikkan akan disimpan dalam suatu class yang bernama document. Nah, strategi dari pembatasan jumlah karakter ini adalah dengan mengeset dokumen baru ke textfield yang ada, dengan pengaturan sedemikian rupa. Saya melakukan override pada method <code>insertString()</code> untuk mengecek apakah karakter yang akan diketikkan akan menghasilkan panjang yang lebih dari limitasi.</p>
<p>Setelah membuat class ini, Anda bisa secara manual memanggilnya dengan</p>
<pre class="brush: java; light: true;">
HQTextFieldUtil.limitTextFieldChar(suatuTextField, suatuAngka);
</pre>
<p>Tapi karena saya lebih suka mendesain GUI dengan <a href="http://www.netbeans.org" target="_blank">NetBeans</a>, saya akan membuat class <code>HQLimitedTextField</code>.</p>
<pre class="brush: java;">
/**
 * DO NOT REMOVE THIS LICENSE
 *
 * This source code is created by Muhammad Fauzil Haqqi.
 * You can use and modify this source code freely but
 * you are forbidden to change or remove this license.
 *
 * Nick  : Haqqi
 * YM    : xp_guitarist
 * Email : fauzil.haqqi@gmail.com
 * Blog  : http://www.fauzilhaqqi.net
 */
package hq.widget;

import hq.utility.HQTextFieldUtil;
import javax.swing.JTextField;

/**
 *
 * @author Haqqi
 */
public class HQLimitedTextField extends JTextField {

  private static final long serialVersionUID = 1L;
  // *******variables area****** //
  private int limit;

  // *****constructors area***** //
  // ********methods area******* //
  /**
   * Get the value of limit
   *
   * @return the value of limit
   */
  public int getLimit() {
    return limit;
  }

  /**
   * Set the value of limit
   *
   * @param limit new value of limit
   */
  public void setLimit(int limit) {
    this.limit = limit;
    firePropertyChange(&quot;limit&quot;, getLimit(), limit);
    HQTextFieldUtil.limitTextFieldChar(this, limit);
  }
}
</pre>
<p>Dengan demikian, setelah proses drag and drop, saya bisa mengatur limitnya menggunakan panel properties yang ada pada NetBeans. Sangat mudah bukan? Happy coding <img src='http://fauzilhaqqi.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><span class="note">Sebenarnya saya akan meng-extend-kan class ini dari HQTextField yang telah diubah tampilannya. Tujuannya adalah agar semua turunannya seragam. Tapi sudahlah, karena memang belum jadi ya ini dulu.</span></p>
<div class="important-grey"><span class="important-title-grey">English keywords:</span>Java tutorial how to limit number of character in JTextField</div>
]]></content:encoded>
			<wfw:commentRss>http://fauzilhaqqi.net/2010/02/membatasi-jumlah-karakter-pada-jtextfield/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Tutorial &#8211; Create Icon Reflection</title>
		<link>http://fauzilhaqqi.net/2010/02/java-tutorial-create-icon-reflection/</link>
		<comments>http://fauzilhaqqi.net/2010/02/java-tutorial-create-icon-reflection/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 13:51:37 +0000</pubDate>
		<dc:creator>Haqqi</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[HQ Library]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Source Code]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.fauzilhaqqi.net/?p=138</guid>
		<description><![CDATA[Just a simple Swing make over
Recently, I just wrote posts about my own world. For now, I will write about another Java Tutorial. Still in the progress of HQLibrary that is suspended, this is another simple class in my library. From the image you can see beside, you will understand that I will show you [...]]]></description>
			<content:encoded><![CDATA[<p><span class="highlight-red"><strong>Just a simple Swing make over</strong></span></p>
<p><a href="http://www.fauzilhaqqi.net/wp-content/uploads/2010/02/13-icon.jpg"><img class="alignleft size-full wp-image-139" style="margin: 8px;" title="13 - icon" src="http://www.fauzilhaqqi.net/wp-content/uploads/2010/02/13-icon.jpg" alt="" width="228" height="206" /></a>Recently, I just wrote posts about my own world. For now, I will write about another Java Tutorial. Still in the progress of HQLibrary <del>that is suspended</del>, this is another simple class in my library. From the image you can see beside, you will understand that I will show you how to make a label that automatically create a reflection if we add an icon. More over, this class can be used in <a href="http://www.netbeans.org" target="_blank">NetBeans</a> GUI form, so you can add an icon using the parameter panel.</p>
<p>Before we go to create an extended class of JLabel, first we need to know how to automatically create a reflection of an image. Using Java 2D API, we can easily finish that problem. Those useful class packaged in <code>java.awt</code> package. We need to do simple 2D manipulation to create a new image that has reflection. So, I created some methods in another class (not in extended JLabel), with <code>static</code> modifier, so that it can be used by another class. I wrapped that methods in a class named <code>HQIconUtil</code>.<br />
<span id="more-138"></span><br />
Here is the source code:</p>
<pre class="brush: java;">
/**
 * DO NOT REMOVE THIS LICENSE
 *
 * This source code is created by Muhammad Fauzil Haqqi.
 * You can use and modify this source code freely but
 * you are forbidden to change or remove this license.
 *
 * Nick  : Haqqi
 * YM    : xp_guitarist
 * Email : fauzil.haqqi@gmail.com
 * Blog  : http://www.fauzilhaqqi.net
 */
package hq.utility;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

/**
 *
 * @author Haqqi
 */
public final class HQIconUtil {
  // *******variables area****** //

  // *****constructors area***** //
  private HQIconUtil() {}

  // ********methods area******* //
  public static BufferedImage convertToBufferedImage(Image image) {
    // get image size
    int width = image.getWidth(null);
    int height = image.getHeight(null);

    // create new buffered image
    BufferedImage result = new BufferedImage(width, height,
        BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = result.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();

    // return the result
    return result;
  }

  public static BufferedImage
      createReflectionBufferedImage(BufferedImage image) {
    // create result image
    BufferedImage result = new BufferedImage(image.getWidth(),
        image.getHeight() * 6 / 4, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = result.createGraphics();

    // paints original image
    g.drawImage(image, 0, 0, null);

    // paints mirrored image
    g.scale(1.0, -1.0);
    g.drawImage(image, 0, -image.getHeight() * 2, null);
    g.scale(1.0, -1.0);

    // move to mirror's origin
    g.translate(0, image.getHeight());

    // create gradien mask
    GradientPaint mask = new GradientPaint(0, 0, new Color(1f, 1f, 1f, 0.5f),
        0, image.getHeight() / 2, new Color(1f, 1f, 1f, 0f));
    g.setPaint(mask);

    // set alpha composite
    g.setComposite(AlphaComposite.DstIn);

    // paint the mask
    g.fillRect(0, 0, image.getWidth(), image.getHeight());

    g.dispose();
    return result;
  }

  public static BufferedImage
      createReflectionBufferedImage(Image image) {
    return createReflectionBufferedImage(convertToBufferedImage(image));
  }
}
</pre>
<p>Because we can only do image manipulation using BufferedImage, first of all we need to convert an Image into BufferedImage one. That is the use of the first method, <code>convertToBufferedImage(Image image)</code>. After that, we parse it to <code>createReflectionBufferedImage(BufferedImage image)</code> method to get the new image with its reflection. The important thing to understand this method is by studying about masking and compositing of Java 2D. The idea here is to composite destination image with a gradient.</p>
<div class="important-blue"><span class="important-title-blue">Further info</span>You can read more about composite <a href="http://java.sun.com/docs/books/tutorial/2d/advanced/compositing.html" target="_blank">here</a>, so that you can create any mask to be composited.</div>
<p>After that, we can create our new class for label. We can easily create it by extending <code>JLabel</code> class of <code>javax.swing</code> package. I use NetBeans to create this class, and others too. So, I just right click and choose to add a new property that saves the reflection icon variable.</p>
<p style="text-align: center;"><a href="http://www.fauzilhaqqi.net/wp-content/uploads/2010/02/13-insert-code.jpg"><img class="size-full wp-image-140 aligncenter" title="13---insert-code" src="http://www.fauzilhaqqi.net/wp-content/uploads/2010/02/13-insert-code.jpg" alt="" width="323" height="155" /></a></p>
<p>Don&#8217;t forget to generate setter and getter method, so that it can be used using GUI parameter panel in NetBeans. I named my class as HQLabel, since it will be included in my HQLibrary.</p>
<p>Here is the source code of HQLabel:</p>
<pre class="brush: java;">
/**
 * DO NOT REMOVE THIS LICENSE
 *
 * This source code is created by Muhammad Fauzil Haqqi.
 * You can use and modify this source code freely but
 * you are forbidden to change or remove this license.
 *
 * Nick  : Haqqi
 * YM    : xp_guitarist
 * Email : fauzil.haqqi@gmail.com
 * Blog  : http://www.fauzilhaqqi.net
 */
package hq.widget;

import hq.utility.HQIconUtil;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;

/**
 *
 * @author Haqqi
 */
public class HQLabel extends javax.swing.JLabel {
  private static final long serialVersionUID = 1L;
  // *******variables area****** //
  private Icon iconReflect;

  // *****constructors area***** //
  /**
   * Default constructor.
   * Set label's text to the bottom center.
   */
  public HQLabel() {
    super();
    setHorizontalTextPosition(SwingConstants.CENTER);
    setVerticalTextPosition(SwingConstants.BOTTOM);
  }

  // ********methods area******* //
  /**
   * Get icon reflection.
   * @return iconReflect - Icon reflection
   */
  public Icon getIconReflect() {
    return this.iconReflect;
  }

  /**
   * Set the icon reflection. Current icon will be
   * replaced by the icon reflection.
   * @param iconReflect
   */
  public void setIconReflect(Icon iconReflect)
      throws IllegalArgumentException {
    // check whether the argument is null
    if (iconReflect == null) {
      firePropertyChange(&quot;iconReflection&quot;, getIconReflect(), iconReflect);
      this.iconReflect = iconReflect;
      setIcon(iconReflect);
    } else {
      if (!(iconReflect instanceof ImageIcon)) {
        throw new IllegalArgumentException();
      }
      firePropertyChange(&quot;iconReflection&quot;, getIconReflect(), iconReflect);
      this.iconReflect = iconReflect;
      BufferedImage img = HQIconUtil.createReflectionBufferedImage(((ImageIcon) iconReflect).getImage());
      setIcon(new ImageIcon(img));
    }
  }
}
</pre>
<p>Ok, those all the way to create a label class that has an image reflection. If you create it correctly, you will see a new parameter in NetBeans parameter editor panel. You can test it by building it and then adding it to a GUI form such as JPanel in NetBeans. You can do that by simply dragging from the file panel. Happy coding&#8230; <img src='http://fauzilhaqqi.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<div class="important-grey"><span class="important-title-grey">Bahasa keywords:</span>Tutorial Java membuat icon dengan efek pantulan cermin.</div>
]]></content:encoded>
			<wfw:commentRss>http://fauzilhaqqi.net/2010/02/java-tutorial-create-icon-reflection/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java Tutorial &#8211; Wrap Text into JLabel</title>
		<link>http://fauzilhaqqi.net/2010/01/java-tutorial-wrap-text-into-jlabel/</link>
		<comments>http://fauzilhaqqi.net/2010/01/java-tutorial-wrap-text-into-jlabel/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 05:21:38 +0000</pubDate>
		<dc:creator>Haqqi</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[HQ Library]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Source Code]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.fauzilhaqqi.net/?p=68</guid>
		<description><![CDATA[Another Java tutorial, hope it will be useful.
Again, this post is from my previous blog post
Coding in Java is my hobby. So I am happy when my subject is about programming in Java. Although I&#8217;m still not a great Java programmer, I want to share this tutorial. It is about how to wrap text in [...]]]></description>
			<content:encoded><![CDATA[<blockquote class="red"><p>Another Java tutorial, hope it will be useful.</p></blockquote>
<p><span class="note">Again, this post is from <a href="http://fauzilhaqqi.blogspot.com/2009/07/java-wrap-text-into-jlabel.html" target="_blank">my previous blog post</a></span></p>
<p><a href="http://www.fauzilhaqqi.net/wp-content/uploads/2010/01/05-javablog.png"><img class="alignleft size-full wp-image-55" style="margin: 8px;" title="Java Blog" src="http://www.fauzilhaqqi.net/wp-content/uploads/2010/01/05-javablog.png" alt="" width="75" height="140" /></a>Coding in Java is my hobby. So I am happy when my subject is about programming in Java. Although I&#8217;m still not a great Java programmer, I want to share this tutorial. It is about how to wrap text in to JLabel, so that whenever the text reach the right side of JLabel, it will go to the next line. Sometimes we will face this condition. For example, when we want to create a bunch of information text such as dialog box that display &#8220;about&#8221; information. We have very long text to display, but if we place it in a JLabel with ordinary process, it will grow horizontally until the end of the text, and never go to the next line.<br />
<span id="more-68"></span><br />
I created a utility class to solve that problem. This class will be added in my own library, if it is frequently used. Like I have said above, the problem is we have a JLabel and we are confused about how to wrap a String in that label. The concept is simple, we just have to arrange it into html form and place new line tag each time the text reach the right side. Ouch, there is another problem come out, how to detect whether it collide or not. Ok, to answer that question, you can read my source below.</p>
<pre class="brush: java;">
/**
 * DO NOT REMOVE THIS LICENSE
 *
 * This source code is created by Muhammad Fauzil Haqqi.
 * You can use and modify this source code freely but
 * you are forbidden to change or remove this license.
 *
 * Nick  : Haqqi
 * YM    : xp_guitarist
 * Email : fauzil.haqqi@gmail.com
 * Blog  : http://www.fauzilhaqqi.net
 */

import java.awt.FontMetrics;
import java.text.BreakIterator;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

/**
 *
 * @author Haqqi
 */
public class LabelUtil {
  // *******variables area****** //

  // *****constructors area***** //
  /**
   * To prevent the creation of the object
   */
  private LabelUtil() {}

  // ********methods area******* //
  /**
   * Wrap array of String into a label
   * @param label
   * @param text
   */
  public static void wrapTextToLabel(JLabel label, String[] text) {
    // measure the length of font in pixel
    FontMetrics fm = label.getFontMetrics(label.getFont());
    // get container width, you must set the fixed width of
    // the container, i.e. JPanel
    int contWidth = label.getParent().getWidth();
    // to find the word separation
    BreakIterator boundary = BreakIterator.getWordInstance();
    // main string to be added
    StringBuffer m = new StringBuffer(&quot;&lt;html&gt;&quot;);
    // loop each index of array
    for(String str : text) {
      boundary.setText(str);
      // save each line
      StringBuffer line = new StringBuffer();
      // save each paragraph
      StringBuffer par = new StringBuffer();
      int start = boundary.first();
      // wrap loop
      for(int end = boundary.next(); end != BreakIterator.DONE;
          start = end, end = boundary.next()) {
        String word = str.substring(start,end);
        line.append(word);
        // compare width with font metrics
        int trialWidth = SwingUtilities
            .computeStringWidth(
            fm, line.toString());
        // if bigger, add new line
        if(trialWidth &gt; contWidth) {
          line = new StringBuffer(word);
          par.append(&quot;&lt;br /&gt;&quot;);
        }
        // add new word to paragraphs
        par.append(word);
      }
      // add new line each paragraph
      par.append(&quot;&lt;br /&gt;&quot;);
      // add paragraph into main string
      m.append(par);
    }
    // closed tag
    m.append(&quot;&lt;/html&gt;&quot;);
    label.setText(m.toString());
  }

  /**
   * Wrap a String into a label
   * @param label
   * @param text
   */
  public static void wrapTextToLabel(JLabel label, String text) {
    String[] newText = new String[] {text};
    wrapTextToLabel(label, newText);
  }
}
</pre>
<p>There are two methods, first is with array of String parameter, and second with single String parameter. There is no difference, just an ordinary overload method. In order to make that method works, there are some adding sequence that you must follow:</p>
<ol>
<li> Create a container (for example, JPanel) with specific <strong>width</strong> by call <code>setSize(width, height)</code> method.</li>
<li> Create an empty JLabel then add it to the panel.</li>
<li> After getting the String (from where you get, it&#8217;s up to you), call <code>LabelUtil.wrapTextToLabel()</code> method.</li>
</ol>
<p>For example, I use this text file to create &#8220;about&#8221; dialog box.</p>
<blockquote class="purple"><p>HQ Library v0.001<br />
http://www.fauzilhaqqi.net &#8211; 2010</p>
<p>Author: Muhammad Fauzil Haqqi</p>
<p>This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing.</p>
<p>This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing.</p>
<p>This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing. This text is just for testing.</p>
<p>If you have some recomendation, please be free to tell me.</p></blockquote>
<p>And then, here is an example of main method to show how LabelUtil works.</p>
<pre class="brush: java;">
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

/**
 *
 * @author Haqqi
 */
public class Tester {
  // *******variables area****** //

  // *****constructors area***** //

  // ********methods area******* //
  public static void main(String[] args) {
    // get default look and feel
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception ex) {
      Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
    }
    // create new JPanel
    JPanel p = new JPanel();
    // set the border
    p.setBorder(BorderFactory.createEtchedBorder());
    // just set its width
    p.setSize(450, 0);
    // get the text from file. you can change this with your own way
    String[] text = FileUtil.fileRead(FileUtil.setFile(&quot;data/about.me&quot;));
    // create new JLabel
    JLabel label = new JLabel();
    // first, add to the panel
    p.add(label);
    // wrap the text
    LabelUtil.wrapTextToLabel(label, text);
    // finally, show the dialog
    JOptionPane.showMessageDialog(null, p, &quot;About this Program&quot;,
        JOptionPane.PLAIN_MESSAGE);
  }
}
</pre>
<p>I read the file using another class of my library, called FileUtil that is already posted <a href="http://www.fauzilhaqqi.net/2010/01/java-tutorial-write-and-read-file/" target="_blank">here</a>. It returns an array of Strings that will be added in LabelUtil method. If your run that program correctly, it should give this display:</p>
<p style="text-align: center;"><a href="http://www.fauzilhaqqi.net/wp-content/uploads/2010/01/06-Wrap-JLabel.png"><img class="size-medium wp-image-69   aligncenter" title="Wrap-JLabel" src="http://www.fauzilhaqqi.net/wp-content/uploads/2010/01/06-Wrap-JLabel-300x227.png" alt="" width="300" height="227" /></a></p>
<p>Enjoy coding in Java!! <img src='http://fauzilhaqqi.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p><span class="notice"><b>Bahasa keywords:</b> Tutorial Java teks JLabel ganti baris otomatis.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://fauzilhaqqi.net/2010/01/java-tutorial-wrap-text-into-jlabel/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java Tutorial &#8211; Write and Read File</title>
		<link>http://fauzilhaqqi.net/2010/01/java-tutorial-write-and-read-file/</link>
		<comments>http://fauzilhaqqi.net/2010/01/java-tutorial-write-and-read-file/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 04:54:29 +0000</pubDate>
		<dc:creator>Haqqi</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[HQ Library]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Source Code]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.fauzilhaqqi.net/?p=54</guid>
		<description><![CDATA[Repost from my previous blog, about Java tutorial.
This post is similar with my post before
Inspired by Eko with his USU Library, now I am creating my own Java library. Of course, I will make it open source, as soon as I think it is finished with version 1.0. Hope that I will focus in this [...]]]></description>
			<content:encoded><![CDATA[<blockquote class="red"><p>Repost from my previous blog, about Java tutorial.</p></blockquote>
<p><span class="note">This post is similar with <a href="http://fauzilhaqqi.blogspot.com/2009/07/java-file-read-and-write.html" target="_blank">my post before</a></span></p>
<p><a href="http://www.fauzilhaqqi.net/wp-content/uploads/2010/01/05-javablog.png"><img class="alignleft size-full wp-image-55" style="margin: 8px;" title="Java Blog" src="http://www.fauzilhaqqi.net/wp-content/uploads/2010/01/05-javablog.png" alt="" width="75" height="140" /></a>Inspired by <a href="http://eecchhoo.wordpress.com" target="_blank">Eko</a> with his USU Library, now I am creating my own Java library. Of course, I will make it open source, as soon as I think it is finished with version 1.0. Hope that I will focus in this project. I think you who are a programmer should make your own library too. Because it will be useful whenever you get a project, and it will be your trademark. You know, although you decide that your library will become open source, the real author is you. So the only one who know most of your program is YOU. Don&#8217;t afraid to be open source.<br />
<span id="more-54"></span><br />
This is my first Java tutorial in this blog. It&#8217;s about one of Java cores, reading and writing into a file. At first, when still newbie in Java, I was confused how to do this thing. Luckily, when reading <a href="http://www.goldenstudios.or.id" target="_blank">GTGE</a>, I found a class that is useful for this task. I learnt from it and make my own FileUtil class. Here it is, you can analyze the code.</p>
<pre class="brush: java;">
/**
 * DO NOT REMOVE THIS LICENSE
 *
 * This source code is created by Muhammad Fauzil Haqqi.
 * You can use and modify this source code freely but
 * you are forbidden to change or remove this license.
 *
 * Nick  : Haqqi
 * YM    : xp_guitarist
 * Email : fauzil.haqqi@gmail.com
 * Blog  : http://www.fauzilhaqqi.net
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 *
 * @author Haqqi
 */
public class FileUtil {
  // *******variables area****** //

  // *****constructors area***** //
  /**
   * To prevent the creation of object
   */
  private FileUtil() {
  }

  // ********methods area******* //
  /**
   * Set the file based on relative path
   * @param filePath Path of the file
   * @return Generated file
   */
  public static File setFile(String filePath) {
    // create new file object
    File file = null;
    // getting the path from working directory
    // you can check the directory by printing it
    String path = System.getProperty(&quot;user.dir&quot;)
        + File.separatorChar + filePath;
    try {
      // construct the file based on the path
      file = new File(path);
    } catch (Exception e) {
      e.printStackTrace();
    }
    // if file is not found, then throw exception
    if (file == null) {
      throw new RuntimeException();
    }
    return file;
  }

  /**
   * Write an Array of String into a file. The written
   * file will be just like a text file.
   * @param text Array of String that want to be written
   * @param file File
   * @return true if success and false if not
   */
  public static boolean fileWrite(String[] text, File file) {
    try {
      // create buffer
      BufferedWriter out = new BufferedWriter(new FileWriter(file));
      PrintWriter writeOut = new PrintWriter(out);
      // writing text to file
      for (int i = 0; i &lt; text.length; i++) {
        writeOut.println(text[i]);
      }
      // close the writer
      writeOut.close();
      return true;
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
  }

  /**
   * Read file from a file
   * @param file
   * @return
   */
  public static String[] fileRead(File file) {
    try {
      // buffered reader
      BufferedReader readIn = new BufferedReader(new FileReader(file));
      // ArrayList to store the string each line
      ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
      // Temporarily object
      String data;
      // Read each line until end of line
      while ((data = readIn.readLine()) != null) {
        list.add(data);
      }
      // closing reader
      readIn.close();
      // return as Array of String
      return list.toArray(new String[0]);
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }
}
</pre>
<p>Using comments I wrote above, you can study how it works by yourself. There are 3 static methods, <code>setFile()</code>, <code>fileWrite()</code>, and <code>fileRead()</code>. The most important thing about reading and writing a file is the location of the file. Using <code>setFile()</code> method, you can easily locate it based on your working directory. After the file is set, you can easily read and write it using another methods above. I think, because my code is already in form of a class, you don&#8217;t need to copy several lines and paste into your class. You can copy the whole class and place it in any package of your project.</p>
<p>Oke, I hope this post will help you out from your problem. Please be free to leave a comment. Lets code in Java!!! <img src='http://fauzilhaqqi.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p><span class="notice"><b>Bahasa keywords:</b> Tutorial Java membaca dan menulis file</span></p>
]]></content:encoded>
			<wfw:commentRss>http://fauzilhaqqi.net/2010/01/java-tutorial-write-and-read-file/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
