java - Overflow JLabel into Another JLabel -
let's have jlabel
sized 20x20
. let's add string "the quick brown fox jumps on lazy dog." sentence long jlabel
, gets replaces ...
. there way overflow jlabel
, first jlabel
have "the quick brown fox" , second jlabel
have "jumps on lazy dog."?
jlabel lbl = new jlabel("the quick brown fox jumps on lazy dog."); lbl.setmaximumsize(new dimension(20, 20)); add(lbl)
produces label says "...".
it easier answer if provided more information.
i'm assuming want 2 labels can placed side side, or in other layout. in case first label's text overflows, second label display remainder.
here's solution:
import java.awt.*; import java.awt.dimension; import java.awt.graphics; import java.awt.renderinghints; import java.awt.event.*; import java.awt.event.componentadapter; import java.awt.event.componentevent; import java.awt.event.windowadapter; import java.awt.event.windowevent; import java.lang.override; import javax.swing.*; import javax.swing.jlabel; import javax.swing.jsplitpane; import javax.swing.swingutilities; public class test extends jframe { public test() { super("test overflowing label"); setdefaultcloseoperation(windowconstants.exit_on_close); setlocation(200, 200); setsize(300, 200); createui(); } private void createui() { jlabel label2 = new jlabel(); jlabel label1 = new overflowinglabel("text truncate since it's long ...", label2); final jsplitpane splitpane = new jsplitpane(jsplitpane.horizontal_split, label1, label2); splitpane.setdividersize(2); addwindowlistener(new windowadapter() { @override public void windowopened(windowevent e) { splitpane.setdividerlocation(.5d); } }); setcontentpane(splitpane); } public static void main(string... args) { swingutilities.invokelater(new runnable() { @override public void run() { test test = new test(); test.setvisible(true); } }); } private class overflowinglabel extends jlabel { private jlabel[] dependantlabels; public overflowinglabel(string text, jlabel... dependantlabels) { super(text); this.dependantlabels = dependantlabels; addcomponentlistener(new componentadapter() { @override public void componentshown(componentevent e) { overflowtext(); } @override public void componentresized(componentevent e) { overflowtext(); } }); } private void overflowtext() { int index = getindextochoptext(); string text = gettext().substring(index); (jlabel dependantlabel : dependantlabels) dependantlabel.settext(text); } private int getindextochoptext() { dimension size = getsize(); fontmetrics metrics = getfontmetrics(getfont()); string text = gettext(); int index = 0; int width = 0; while ((index < text.length()) && (width < size.width)) { int charwidth = metrics.charwidth(text.charat(index)); if ((width + charwidth) > size.width) break; index ++; width += charwidth; } return index; } @override public dimension getminimumsize() { dimension size = super.getminimumsize(); return new dimension(0, size.height); } @override protected void paintcomponent(graphics g) { graphics2d g2d = (graphics2d)g.create(); rectangle clip = g2d.getclipbounds(); g2d.setrenderinghint(renderinghints.key_text_antialiasing, renderinghints.value_text_antialias_lcd_hrgb); g2d.setcolor(getbackground()); g2d.fillrect(clip.x, clip.y, clip.width, clip.height); g2d.setcolor(getforeground()); g2d.setfont(getfont()); fontmetrics metrics = g2d.getfontmetrics(); int baseline = metrics.getascent() + metrics.getleading(); int index = getindextochoptext(); g2d.drawstring(gettext().substring(0, index), 0, (getheight() + baseline) / 2 - 1); g2d.dispose(); } } }
here screen shots demonstrate behavior:
Comments
Post a Comment