Here is one of my source codeNow 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:
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, 2009Posted by Haqqi at 6:03 PM
Labels: English, My Source Code
Subscribe to:
Post Comments (Atom)
4 comments:
Wah, kamu memang temanQ....
Lanjutkan,Qi!! Q tunggu nama2 muridQ menghiasi PCMedia selanjutnya... ^^
@^: No comment...
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?
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?
Post a Comment