[英]How can I stop the tree view expand/collapse icons from fading when losing focus in Eclipse?
[英]How can i expand a textfield when it's in focus and when it's out of focus it should collapse?
我想使用一個文本字段,當用戶輸入一些數據時,當用戶完成編輯並移出文本字段時,該文本字段將展開,而該文本字段應該折疊。 之后,當用戶將注意力集中在文本字段上時,它應該像工具提示一樣展開。 關於此的任何指示將對我有幫助。
只需在您的文本字段中添加一個FocusListener
即可:
text.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
text.setSize(); // put in the size you want
}
@Override
public void focusLost(FocusEvent e) {
text.setSize(); // put in the size you want
}
});
請注意,為使setSize
正常工作, text
父級不能具有布局。
這段代碼應該使您了解如何執行此操作:
public static void main(String[] args)
{
Display display = Display.getDefault();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
final Text text = new Text(shell, SWT.BORDER | SWT.MULTI);
text.setText("edasdasdas\n\nasdasda\n\nasdasd");
final GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
data.heightHint = 100;
text.setLayoutData(data);
text.addListener(SWT.FocusIn, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
data.heightHint = 100;
shell.layout(true);
}
});
text.addListener(SWT.FocusOut, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
data.heightHint = 50;
shell.layout(true);
}
});
Button button = new Button(shell, SWT.PUSH);
button.setText("Button");
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
當焦點丟失/獲得時,它基本上會更改GridData
的heightHint
。 之后,您需要重新布置父級。
您必須自己對“聚焦高度”和“非聚焦高度”值進行一些調整。
以下是一些屏幕截圖:
重點關注:
沒有重點:
只需按一下Button
即可失去焦點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.