2014-04-04 63 views
1

我有一个可编辑的SWT组合。用户可以从下拉列表中选择一个项目或输入新的文本。我的代码也做了一些数据验证,如果输入的文本无效,我想强调焦点到组合以及突出显示输入的文本。SWT可编辑组合 - 高亮文本

我的问题是:有没有办法突出显示组合中的所有输入文本?不幸的是,我认为没有“selectText”方法...

在此先感谢。

我试图巴兹的建议,但不知什么原因,该代码同时工作在一个单独的测试项目,同样的代码并不在我的计划工作:

comboViewerRes = new ComboViewer(this, SWT.DROP_DOWN); 
    comboRes = comboViewerRes.getCombo(); 
    comboRes.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 
      1, 1)); 

    comboRes.addListener(SWT.KeyUp, new Listener() { 
     @Override 
     public void handleEvent(Event e) { 
      if (e.keyCode == SWT.SPACE) { 
       Rectangle bounds = comboRes.getBounds(); 
       Point point = new Point(bounds.x, bounds.x + bounds.width); 
       System.out.println(point); 
       comboRes.setSelection(point); 
      } 
     } 
    }); 
+0

发布您尝试使用的代码。有些人会纠正它并回答 –

回答

1

好了,有做没有直观的方式但是,这个Combo包含一个称为setSelection(Point)的方法。这种方法的javadoc的规定:

设置在接收器中的文本字段的选择以通过其x坐标是该选择的开始和其y坐标是该选择的端部的参数指定的范围内。

为了选择一切,你需要做的就是创建一个Point,设置它的x价值的Combo的左边缘和y协调到Combo的右边缘。

下面是一个代码示例:

public static void main(String[] args) 
{ 
    final Display display = new Display(); 
    Shell shell = new Shell(display); 
    shell.setLayout(new FillLayout()); 
    shell.setText("ExpandBar Example"); 

    final ComboViewer viewer = new ComboViewer(shell, SWT.DROP_DOWN); 

    viewer.getCombo().addListener(SWT.KeyUp, new Listener() 
    { 
     @Override 
     public void handleEvent(Event e) 
     { 
      if(e.keyCode == SWT.SPACE) 
      { 
       Rectangle bounds = viewer.getCombo().getBounds(); 
       Point point = new Point(bounds.x, bounds.x + bounds.width); 
       viewer.getCombo().setSelection(point); 
      } 
     } 
    }); 

    viewer.setContentProvider(ArrayContentProvider.getInstance()); 
    viewer.setLabelProvider(new LabelProvider() 
    { 
     @Override 
     public String getText(Object element) 
     { 
      if (element instanceof String) 
      { 
       return element.toString(); 
      } 
      return super.getText(element); 
     } 
    }); 

    String[] persons = new String[] { "this", "is", "a", "test" }; 

    viewer.setInput(persons); 

    shell.pack(); 
    shell.open(); 

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

可以正常输入。一旦你按空间,代码将选择到目前为止输入的所有内容。

下面是一些截图:

enter image description here enter image description here

+0

该代码本身工作正常。但是,相同的代码在使用ComboViewer的代码中不起作用。我尝试了'Combo combo = comboViewer.getCombo();'然后按照你的建议添加了监听器 - 在运行时没有任何事情发生。 'combo.setSelection'这一行实际上是运行的(我放了一个'System.out.println(bounds);'来确认,我不确定它为什么在我的程序中不起作用。这可能是ComboViewer的“特性” ? – user3229864

+0

@ user3229864那么,你没有在你的问题中指定你正在使用'ComboViewer'而不是'Combo',我会研究它 – Baz

+0

@ user3229864更新的代码它现在使用'ComboViewer' 。相同的代码仍然可以正常工作。 – Baz

0

其实,我发现是,combo.setSelection()应该采取文本字符串作为第一个参数的起始索引的一个点,而第二个参数为结束索引,即在文本中选择前两个字符做,combo.setSelection(new Point(0,2));并选择所有文本做combo.setSelection(new Point(0, combo.getText().length()));

为什么它的工作对巴兹和不user3229864,可能是事实,是b az的组合在坐标0上,而user3229864不是。

希望这会有所帮助!