SWT(JFace)体验之List演示汇总第1/2页

代码如下:
DropDownAndSimple.java


代码如下:

package swt_jface.demo3;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class DropDownAndSimple {

Display display = new Display();
Shell shell = new Shell(display);
public DropDownAndSimple() {
RowLayout rowLayout = new RowLayout();
rowLayout.spacing = 15;
rowLayout.marginWidth = 15;
rowLayout.marginHeight = 15;

shell.setLayout(rowLayout);

Combo comboDropDown = new Combo(shell, SWT.DROP_DOWN | SWT.BORDER);
Combo comboSimple = new Combo(shell, SWT.SIMPLE | SWT.BORDER);

for(int i=0; i<3; i++) {
comboDropDown.add("item " + i);
comboSimple.add("item " + i);
}
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new DropDownAndSimple();
}
}

SampleCombo.java


代码如下:

package swt_jface.demo3;
import java.util.Arrays;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
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 SampleCombo {

Display display = new Display();
Shell shell = new Shell(display);
public SampleCombo() {

shell.setLayout(new GridLayout(2, false));
(new Label(shell, SWT.NULL)).setText("Select your favorite programming language: ");
//final CCombo combo = new CCombo(shell, SWT.FLAT);
final Combo combo = new Combo(shell, SWT.NULL);
String[] languages = new String[]{"Java", "C", "C++", "SmallTalk"};
Arrays.sort(languages);
for(int i=0; i<languages.length; i++)
combo.add(languages[i]);
//combo.add("Perl", 5);
//combo.setItem(5, "Perl");

combo.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Selected index: " + combo.getSelectionIndex() + ", selected item: " + combo.getItem(combo.getSelectionIndex()) + ", text content in the text field: " + combo.getText());
}
public void widgetDefaultSelected(SelectionEvent e) {
System.out.println("Default selected index: " + combo.getSelectionIndex() + ", selected item: " + (combo.getSelectionIndex() == -1 ? "<null>" : combo.getItem(combo.getSelectionIndex())) + ", text content in the text field: " + combo.getText());
String text = combo.getText();
if(combo.indexOf(text) < 0) { // Not in the list yet.
combo.add(text);
// Re-sort
String[] items = combo.getItems();
Arrays.sort(items);
combo.setItems(items);
}
}
});
shell.pack();
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new SampleCombo();
}
}

SampleList.java


代码如下:

package swt_jface.demo3;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class SampleList {

Display display = new Display();
Shell shell = new Shell(display);
public SampleList() {
RowLayout rowLayout = new RowLayout();
shell.setLayout(rowLayout);
(new Label(shell, SWT.NULL)).setText("What programming languages are you proficient in? ");
final List list = new List(shell, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);
String[] languages = new String[]{"Java", "C", "C++", "SmallTalk"};
for(int i=0; i<languages.length; i++)
list.add(languages[i]);
list.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
System.err.println(list.getSelectionIndex());
int[] indices = list.getSelectionIndices();
String[] items = list.getSelection();
StringBuffer sb = new StringBuffer("Selected indices: ");
for(int i=0; i < indices.length; i++) {
sb.append(indices[i]);
sb.append("(");
sb.append(items[i]);
sb.append(")");
if(i == indices.length-1)
sb.append('.');
else
sb.append(", ");
}
System.out.println(sb.toString());
}
public void widgetDefaultSelected(SelectionEvent e) {
int[] indices = list.getSelectionIndices();
String[] items = list.getSelection();
StringBuffer sb = new StringBuffer("Default selected indices: ");
for(int i=0; i < indices.length; i++) {
sb.append(indices[i]);
sb.append("(");
sb.append(items[i]);
sb.append(")");
if(i == indices.length-1)
sb.append('.');
else
sb.append(", ");
}
System.out.println(sb.toString());
}
});

list.selectAll();
//list.select(1);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

public static void main(String[] args) {
new SampleList();
}
}

SingleMultiLists.java


代码如下:

package swt_jface.demo3;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class SingleMultiLists {

Display display = new Display();
Shell shell = new Shell(display);
public SingleMultiLists() {

GridLayout gridLayout = new GridLayout(2, true);
shell.setLayout(gridLayout);
(new Label(shell, SWT.NULL)).setText("SINGLE");
(new Label(shell, SWT.NULL)).setText("MULTI");
List singleSelectList = new List(shell, SWT.BORDER);
List mutliSelectList = new List(shell, SWT.MULTI | SWT.BORDER);
String[] items = new String[]{"Item 1", "Item 2", "Item 3", "Item 4"};
for(int i=0; i<items.length; i++) {
singleSelectList.add(items[i]);
mutliSelectList.add(items[i]);
}
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new SingleMultiLists();
}
}

当前1/2页 12下一页阅读全文

(0)

相关推荐

  • SWT(JFace)体验之List演示汇总第1/2页

    代码如下:DropDownAndSimple.java 复制代码 代码如下: package swt_jface.demo3; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; p

  • SWT(JFace)体验之GridLayout布局

    GridLayout布局 GridLayout 布局的功能非常强大,也是笔者常用的一种布局方式.GridLayout是网格式布局,它把父组件分成一个表格,默认情况下每个子组件占据一个单元格的空间,每个子组件按添加到父组件的顺序排列在表格中.GridLayout提供了很多的属性,可以灵活设置网格的信息.另外,GridLayout 布局提供了GridData类,子组件可以设置相应的GridData,例如 "dogPhoto.setLayoutData(gridData)",GridData

  • SWT(JFace)体验之StyledText类

    WrapLines.java 复制代码 代码如下: package swt_jface.demo4; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.

  • SWT(JFace)体验之复制粘贴

    演示代码如下: 复制代码 代码如下: package swt_jface.demo11; import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.Clipboard; import org.eclipse.swt.dnd.RTFTransfer; import org.eclipse.swt.dnd.TextTransfer; import org.eclipse.swt.dnd.Transfer; import org.eclipse.sw

  • SWT(JFace)体验之Sash(活动控件)

    演示代码如下: 复制代码 代码如下: package swt_jface.demo9; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Sash; import org.eclipse.swt

  • SWT(JFace)体验之Slider,Scale

    Slider: 复制代码 代码如下: package swt_jface.demo8; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse

  • SWT(JFace)体验之FillLayout布局

    FillLayout布局 FillLayout是非常简单的一种布局方式,它会以同样大小对父组件中的子组件进行布局,这些子组件将以一行或一列的形式排列.一般来说,用户可以在任务栏.工具栏中放置FillLayout布局,通过FillLayout布局对子组件进行定位,也可以当子组件只有一个组件时,通过FillLayout布局填充整个父组件的空间. FillLayout的风格 FillLayout布局中,可以把子组件按水平或垂直的方式进行排列,这些风格是当创建FillLayout实类时以参数形式指定的.

  • SWT(JFace)体验之ApplicationWindow

    测试代码如下: 复制代码 代码如下: package swt_jface.demo; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.layout.GridLayou

  • SWT(JFace) 体验之FontRegistry

    复制代码 代码如下: package swt_jface.demo; import org.eclipse.jface.resource.FontRegistry; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.ecli

  • SWT(JFace)体验之圆环状(戒指型)

    演示代码: 复制代码 代码如下: package swt_jface.demo1; import org.eclipse.swt.SWT; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.events.MouseMoveListener; import org.eclipse.swt.graphics.Point; impor

随机推荐