我有一个org.eclipse.swt.text
小部件。有时窗口小部件不够大,无法显示其中的所有信息,有没有办法改变显示信息的方式而不改变实际内容?
即,它应显示“1234 ... 4321”,但当我getText()
时,它应返回实际值“123456654321”。
或者是否有可以处理此问题的不同SWT窗口小部件?
答案 0 :(得分:9)
不幸的是,您无法使用org.eclipse.swt.widgets.Text
,因为SWT实际上适用于本机窗口小部件集,并且在大多数操作系统上,本机文本窗口小部件可以具有单个状态,即值。
解决方法是使用org.eclipse.swt.widgets.Text
标志创建SWT.MULTI|SWT.WRAP
或将原始文本小部件包装在实用程序类中。
<强> >>Output
强>
<强> >>Sample Wrapper Code
强>
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
public class MyText
{
private Text text;
private String value;
public MyText(Composite parent, int style) {
text = new Text(parent, style);
}
public Text getControl() {
return text;
}
public String getText() {
return value;
}
public void setText(String value) {
if(text != null && !text.isDisposed()){
this.value = value;
text.setText(shortenText(value, text.getParent()));
}
}
private String shortenText(String textValue, Control control)
{
if (textValue == null) {
return null;
}
GC gc = new GC(control);
int maxWidth = control.getBounds().width - 25;
int maxExtent = gc.textExtent(textValue).x;
System.out.println(maxWidth + "\n" + maxExtent);
if (maxExtent < maxWidth) {
gc.dispose();
return textValue;
}
int length = textValue.length();
int charsToClip = Math.round(0.95f*length * (1 - ((float)maxWidth/maxExtent)));
int pivot = length / 2;
int start = pivot - (charsToClip/2);
int end = pivot + (charsToClip/2) + 1;
while (start >= 0 && end < length) {
String s1 = textValue.substring(0, start);
String s2 = textValue.substring(end, length);
String s = s1 + "..." + s2;
int l = gc.textExtent(s).x;
if (l < maxWidth) {
gc.dispose();
return s;
}
start--;
end++;
}
gc.dispose();
return textValue;
}
}
注意 shortenText()
方法的实现。
<强> >>Main
强>
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class TextWidgetTest
{
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Text Widget Test");
shell.setLayout(new GridLayout());
shell.setSize(300, 200);
final MyText text = new MyText(shell, SWT.SINGLE|SWT.BORDER);
text.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
text.setText("A very very very very very very very very very very very very very very very very very very very very long text");
Button show = new Button(shell, SWT.PUSH);
show.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
show.setText("Show Actual Text");
final Text console = new Text(shell, SWT.BORDER|SWT.MULTI|SWT.WRAP|SWT.V_SCROLL);
console.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
show.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
console.setText(text.getText());
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
答案 1 :(得分:1)
使用SWT.MULTI | SWT.WRAP标志创建控件是否适合您?这样,您就可以获得包装文本的多行控件。
以下是如何操作:
Text myText = new Text(parent, SWT.MULTI|SWT.WRAP);