Java 如何在Swt组合框中设置默认值?

Java 如何在Swt组合框中设置默认值?,java,swt,Java,Swt,我有一个组合框,它将处于只读模式。我想为该组合框设置一个默认值,指示组合框的用途(例如:一个位置组合框,默认文本为'location',组合框中有许多其他项目,如美国、印度、英国等)。 注意:默认值不应是组合框中的项目之一。 我知道如果组合框处于只读模式是不可能的。 如果有任何可行的解决方法,请告诉我 如下图所示,有一个组合框具有不同的变体,如a、B、C、D等,但组合框具有默认标签“Variant” 这可以通过使用。如果使用设置组合上的项目,在使用之前,您将在组合中看到一个默认值,该值不是列表中

我有一个组合框,它将处于只读模式。我想为该组合框设置一个默认值,指示组合框的用途(例如:一个位置组合框,默认文本为'location',组合框中有许多其他项目,如美国、印度、英国等)。 注意:默认值不应是组合框中的项目之一。 我知道如果组合框处于只读模式是不可能的。 如果有任何可行的解决方法,请告诉我

如下图所示,有一个组合框具有不同的变体,如a、B、C、D等,但组合框具有默认标签“Variant”


这可以通过使用。如果使用设置组合上的项目,在使用之前,您将在组合中看到一个默认值,该值不是列表中的项目之一

请注意,当调用
getSelectionIndex()
时,返回的值将是
-1
,因为尚未选择任何项目,并且一旦选择了项目,默认值将不再存在

public class CComboDefaultTextTest {

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

        final Composite baseComposite = new Composite(shell, SWT.NONE);
        baseComposite
                .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        baseComposite.setLayout(new GridLayout());

        final CCombo combo = new CCombo(baseComposite, SWT.READ_ONLY
                | SWT.BORDER);
        combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        // Be sure to do this before calling setText()
        combo.setItems(new String[] { "item 1", "item 2", "item 3" });
        combo.setText("Default");

        System.out.println(combo.getSelectionIndex());

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

}
结果:


这可以通过使用。如果使用设置组合上的项目,在使用之前,您将在组合中看到一个默认值,该值不是列表中的项目之一

请注意,当调用
getSelectionIndex()
时,返回的值将是
-1
,因为尚未选择任何项目,并且一旦选择了项目,默认值将不再存在

public class CComboDefaultTextTest {

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

        final Composite baseComposite = new Composite(shell, SWT.NONE);
        baseComposite
                .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        baseComposite.setLayout(new GridLayout());

        final CCombo combo = new CCombo(baseComposite, SWT.READ_ONLY
                | SWT.BORDER);
        combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        // Be sure to do this before calling setText()
        combo.setItems(new String[] { "item 1", "item 2", "item 3" });
        combo.setText("Default");

        System.out.println(combo.getSelectionIndex());

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

}
结果:


要显示超出只读组件值的内容吗?@UsagiMiyamoto是要显示超出只读组件值的内容吗?@UsagiMiyamoto是