java - Restrict size of Text widget -
we have text
widget input changed dynamically. size computed after setting new input. size should little possible
this works fine. wondering if in way possible restrict size of text
. if lot of lines added text, takes of composite. there way update size of text
widget after changing text, maximum value?
so far, tried add together resize listener on text
. problem though, widget resized, space taken buttom. other content above covered anyways.
you can utilize griddata#heighthint
restrict size of text
. here illustration restricts height 3 (3) lines:
public static void main(string[] args) { display display = new display(); shell shell = new shell(); shell.settext("stackoverflow"); shell.setlayout(new gridlayout(1, false)); final text text = new text(shell, swt.border | swt.multi | swt.v_scroll); text.setlayoutdata(new griddata(swt.fill, swt.top, true, true)); text.addlistener(swt.modify, new listener() { private int height = 0; @override public void handleevent(event arg0) { int newheight = computeheight(text, 3); if (newheight != height) { height = newheight; griddata griddata = (griddata) text.getlayoutdata(); griddata.heighthint = height; text.setlayoutdata(griddata); text.getparent().layout(true, true); } } }); shell.pack(); shell.setsize(400, 300); shell.open(); while (!shell.isdisposed()) { if (!display.readanddispatch()) { display.sleep(); } } display.dispose(); } private static int computeheight(text text, int maxheight) { int height = text.gettext().split("\n", -1).length; homecoming math.min(height, maxheight) * text.getlineheight(); }
java layout swt
No comments:
Post a Comment