我需要SWT帮助。 我想在一个组中创建一个滚动列表。 我使用以下代码,但组的大小是可变的,并且Scrollbars不可用。我想要一个具有给定大小的List,如果列表中有太多条目,我想让一个滚动条向下滚动。
super(parent, style);
this.setLayout(new GridLayout(1,false));
Group grpSettings = new Group(this, SWT.NONE);
grpSettings.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
grpSettings.setText("Settings");
grpSettings.setLayout(new GridLayout(1, false));
grpSettings.setSize(200, 200);
Label nameLabel = new Label(grpSettings, SWT.NONE);
nameLabel.setText("Choose");
ScrolledComposite scroll = new ScrolledComposite(grpSettings, SWT.V_SCROLL | SWT.H_SCROLL);
scroll.setSize(200, 200);
scroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
scroll.setAlwaysShowScrollBars(true);
final List list = new List(scroll, SWT.NONE); // Create a List with a vertical ScrollBar
// Add a bunch of items to it
for (int i=1;i < 50 ;i++ ){
list.add("Ex" + i);
}
scroll.setContent(list);
scroll.setExpandHorizontal(true);
scroll.setExpandVertical(true);
list.addListener(SWT.Selection, new Listener () {
public void handleEvent (Event e) {
int es = list.getSelectionIndex();
System.out.println(es);
}
});
}
答案 0 :(得分:1)
List
窗口小部件有自己的滚动条,因此无需将其包装到ScrolledComposite
中。
与下面的代码段相似,只需使用List
样式标记
V_SCROLL
即可
public static void main( String[] args ) {
Display display = new Display();
Shell shell = new Shell( display );
shell.setLayout( new FillLayout() );
Group group = new Group( shell, SWT.NONE );
group.setText( "Group" );
group.setLayout( new GridLayout( 1, false ) );
Label label = new Label( group, SWT.NONE );
label.setText( "Choose" );
List list = new List( group, SWT.V_SCROLL );
list.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
for( int i = 0; i < 128; i++ ) {
list.add( "Item " + i );
}
shell.setSize( 300, 300 );
shell.open();
while( !shell.isDisposed() ) {
if( !display.readAndDispatch() )
display.sleep();
}
display.dispose();
}