java - jface 表的布局设置

标签 java user-interface checkbox swt jface

我创建了一个 CheckboxTableViewer 来显示内容列表,如下图所示。问题是表格的滚动条无法正常工作。也许我缺少表格的一些布局参数?

Table

这是代码:

public class TableViewerClass {


CheckboxTableViewer tableViewer;

private GridData gridData;

private List<String> checkListNames = new ArrayList<String>();

public TableViewerClass(Shell parent){

    checkListNames.add("Function Trace 1");
    checkListNames.add("Function Trace 2");
    checkListNames.add("Function Trace 3");
    checkListNames.add("Function Trace 4");
    checkListNames.add("Function Trace 5");
    checkListNames.add("Function Trace 6");

    createCheckViewer(parent);
    createControlBox(parent);
}


private void createCheckViewer(Composite parent){

    tableViewer = CheckboxTableViewer.newCheckList(parent, SWT.SINGLE| SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);

    gridData = new GridData(SWT.TOP, SWT.TOP, false, false, 3, 1);

    tableViewer.add(checkListNames.get(0));
    tableViewer.add(checkListNames.get(1));
    tableViewer.add(checkListNames.get(2));
    tableViewer.add(checkListNames.get(3));
    tableViewer.add(checkListNames.get(4));
    tableViewer.add(checkListNames.get(5));


    tableViewer.add(checkListNames.get(0));
    tableViewer.add(checkListNames.get(1));
    tableViewer.add(checkListNames.get(2));
    tableViewer.add(checkListNames.get(3));
    tableViewer.add(checkListNames.get(4));
    tableViewer.add(checkListNames.get(5));

    tableViewer.add(checkListNames.get(0));
    tableViewer.add(checkListNames.get(1));
    tableViewer.add(checkListNames.get(2));
    tableViewer.add(checkListNames.get(3));
    tableViewer.add(checkListNames.get(4));
    tableViewer.add(checkListNames.get(5));

    // define layout for the viewer
    GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false, 3, 1);
    tableViewer.getControl().setLayoutData(gridData);


    final Table table = tableViewer.getTable();

    TableLayout layout = new TableLayout();


    TableViewerColumn col = new TableViewerColumn(tableViewer, SWT.LEAD);
    col.getColumn().setText("Text");
    layout.addColumnData(new ColumnWeightData(500));

    table.setLayout(layout);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
}

private void createControlBox(Composite parent){

    Group group = new Group(parent, SWT.NONE);
    group.setText("Control Box");
    gridData = new GridData(SWT.LEFT, SWT.TOP, true, false,1,1);
    group.setLayoutData(gridData);

    FillLayout fillLayout = new FillLayout();
    fillLayout.type = SWT.VERTICAL;
    group.setLayout(fillLayout);

    Button button = new Button(group, SWT.PUSH);
    button.setText("Select All");

    Button button2 = new Button(group, SWT.PUSH);
    button2.setText("Deselect All");

    button.addSelectionListener(this.getSelectAllHandler());
    button2.addSelectionListener(this.getDeselectAllHandler());
}

private SelectionListener getSelectAllHandler(){

    SelectionListener selectAll = new SelectionListener(){
        @Override
        public void widgetSelected(SelectionEvent e) {

            tableViewer.setAllChecked(true);
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            // TODO Auto-generated method stub

        }
    };

    return selectAll;

}

private SelectionListener getDeselectAllHandler(){

    SelectionListener deselectAll = new SelectionListener(){
        @Override
        public void widgetSelected(SelectionEvent e) {

            tableViewer.setAllChecked(false);
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            // TODO Auto-generated method stub

        }
    };

    return deselectAll;

}


public static void main(String[] args) {


    Display display = new Display();
    final Shell shell = new Shell(display);

    shell.setSize(500, 400);

    GridLayout layout = new GridLayout(4, false);

    shell.setLayout(layout);

    shell.setText("JFace Table Example");
    new TableViewerClass(shell);

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

 }

最佳答案

shell.pack() 调用重新计算 shell 的大小以使其适合内容。

要限制表格的大小,您需要在表格上指定高度提示:

GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false, 3, 1);

gridData.heightHint = 400;  // Vertical size you want

tableViewer.getControl().setLayoutData(gridData);

关于java - jface 表的布局设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28410695/

相关文章:

java - 日期选择器和时间选择器 - 设置最大值和最小值

c++ - Qt 如何使用组合框选择更新 GUI QFrame

javascript - 如何确定在 HTML 表单中选中了哪些复选框?

javascript - 获取所有选中的复选框并将其设置为隐藏输入

java - React Native android 0.19.0 如何添加节点模块依赖项?

java - android getDate 从存储在字符串字段中的毫秒

java - 替换 jpa 中的月、年、日期函数

c - 如何使用 Gtk 获取屏幕的大小?

java - 在后台更新 JavaFX 窗口

python - Django:编写一个 View 来删除带有复选框的项目