Welcome to HQ's Blog

Hello World! If you've visited this blog before, welcome back. Please help me what article do you like by give it a comment. But if you're new with this blog, please feel free to stay ahead for a shortwhile, to read my articles. I assure you will get something by read it carefully. Haha...

Ok ok... By the way, this blog belongs to me, Haqqi. Yeah, that is my nickname. If you want to know about me, please read the top sidebar titled "About Me". If you want to know MORE about me, please feel free to add my Messenger (xp_guitarist) and chat with me. I am Indonesian. My English is not so good, so please don't laugh if my 'syntax' is wrong. But sometimes I will post my blog in Bahasa Indonesia (I think most of them will).

The reason I create this blog is, I want to share everything I can share. From my experience, computer program I used, my algorithms and source codes, and some useful infos and tips. So, if you want to request something I can, feel free to ask. And if you also want to share me something, I always open my hand, haha. Anyway, WELCOME TO MY BLOG!

This site is best viewed with Mozilla Firefox with resolution 1024x800 or more

Page copy protected against web site content infringement by Copyscape

ANNOUNCEMENT!!!

This blog is dead!!! I moved it to my new blog in http://haqqi.net. Thank you for reading this blog. Hope you will read my new blog too.


Tutorial Java : Wrap Text into JLabel

Jul 18, 2009

Here is one of my source code

Now I'm writing for my next article in PC Media. In my program, I need to read a text file, and then parse it to JLabel to display the text. It is a long text file that contain not only a single sentence, but complex text with paragraf formatted. If I easily parse the text into the JLabel, its width will be as long as the text, can you imagine? So, I need to know how to wrap text in my JLabel. After browsing on the internet, I only found some basic method, and I rearrange it to be more suited for my program. And I think you can use this idea too.

Before you start to learn this post, make sure you have read the previous article that tell about reading a file. Using that method, I can get Array of String of my text file. After that, I need to wrap that Array of String into JLabel. So, I create this static method. I put it into a class named LabelUtil. One more thing, you have to define the container of the JLabel. If not, this method will return error.

It's easy to do that. You can just create a JPanel and add the JLabel into that panel, so that panel will be JLabel's container. You have to call setSize(int, int); of the container firstly.

Here is the method:


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

public class LabelUtil {
/**
* Make the text automatically wrap into JLabel
* @param label JLabel
* @param text Array of String
*/
private void wrapLabelText(JLabel label,
String[] text) {
// measure the length of font in swing component
FontMetrics fm = label.getFontMetrics(
label.getFont());
// size of parent container
Container container = label.getParent();
int containerWidth = container.getWidth();
// to find the word separation
BreakIterator boundary = BreakIterator
.getWordInstance();
// main string to be added
StringBuffer m = new StringBuffer("<html>");
// 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
int trialWidth = SwingUtilities
.computeStringWidth(
fm, line.toString());
// if bigger, add new line
if(trialWidth > containerWidth) {
line = new StringBuffer(word);
par.append("<br>");
}
par.append(word);
}
// add new line each paragraph
par.append("<br>");
// add paragraph into main string
m.append(par);
}
// closed tag
m.append("</html>");
label.setText(m.toString());
}
}


Suppose that my text file is like this:

VT Alpha - Program Pemilihan
http://fauzilhaqqi.blogspot.com - 2009

Programmer : Muhammad Fauzil Haqqi

Program ini adalah program untuk pemilihan dengan metode voting. Cara pembuatan program ini telah ditulis dalam bentuk artikel pada majalah PC Media. Source code program ini bebas untuk dipakai, diubah, maupun disebarkan secara gratis.

Programmer tidak memberikan garansi jika nantinya ada kesalahan yang terjadi selama penggunaan program ini. Programmer juga tidak bertanggung jawab terhadap penyalahgunaan program ini dalam aplikasi nyatanya.

Jika Anda memiliki saran maupun pertanyaan tentang program ini, silahkan menghubungi programmer melalui alamat blog di atas.


So, after I get the Array of String of my text file by using static method FileUtil.fileRead();, I just parse the Array to method wrapLabelText();. So easy, isn't it???

I use this code to display a dialog into my frame:

JPanel pane = new JPanel();
pane.setBorder(BorderFactory
.createEtchedBorder());
pane.setSize(270, 0);
// filepath
String[] text = FileUtil.fileRead(
FileUtil.setFile("data/about.vta"));
JLabel label = new JLabel();
pane.add(label);
LabelUtil.wrapLabelText(label, text);
JOptionPane.showMessageDialog(this, pane,
"Tentang Kami",
JOptionPane.PLAIN_MESSAGE);

After do everything I can do, this is the displayed dialog:

4 comments:

Mutiara Aisyah said...

Wah, kamu memang temanQ....

Lanjutkan,Qi!! Q tunggu nama2 muridQ menghiasi PCMedia selanjutnya... ^^

Haqqi said...

@^: No comment...

Anonymous said...

good evening bros. I'm honestly into shoes and I had been looking for the sake of that meticulous make. The prices seeking the sneakers are about 240 bucks on every site. But definitively I found this location selling them for the benefit of half price. I really love those [url=http://www.shoesempire.com]prada sneakers[/url]. I will probably order them. what can you tell me about these?

Anonymous said...

good day dudes. I'm really into shoes and I was searching for that particular brand. The prices due to the fact that the velcros are about 190 bucks on every site. But finally I base this locate selling them for the benefit of half price. I absolutely want these [url=http://www.shoesempire.com]prada sneakers[/url]. I will absolutely order these. what do you think?