gpt4 book ai didi

java - 单击下拉按钮后,如何清除组合框中选择的默认值?

转载 作者:行者123 更新时间:2023-12-02 09:36:36 25 4
gpt4 key购买 nike

我正在尝试在组合框中设置默认值,因为组合框还支持搜索默认值默认情况下被视为搜索字符串,我需要执行2个操作,即清除默认值和显示列表中的其他条目。那么,如何在单击下拉按钮时清除默认文本,以便我的所有列表值都可见。

最佳答案

您可以利用SWT mouseDown 事件来实现此目的。请参阅Mouse Adapter options

下面是示例代码,当您单击下拉按钮时,它将清除选择。

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class ComboMainClass
{

public static void main(final String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);

RowLayout rowLayout = new RowLayout();
rowLayout.marginLeft = 10;
rowLayout.marginTop = 10;
shell.setLayout(rowLayout);

Label label = new Label(shell, SWT.NONE);
label.setText("Select Items:");

Combo combo = new Combo(shell, SWT.DROP_DOWN);
String[] items = new String[] { "Item One", "Item two", "Item three" };
combo.setItems(items);

combo.addMouseListener(new MouseAdapter()
{
@Override
public void mouseDown(final MouseEvent e)
{
combo.setText("");
}
});

shell.setText("SWT Combo");
shell.setSize(400, 200);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}

}

关于java - 单击下拉按钮后,如何清除组合框中选择的默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57459627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com