2

我正在尝试找到一种方法来制作一个看起来像本机表头组件的 SWT 组件,因此在 Windows 7 中:

Windows 7 列标题

我希望它也具有与本机组件相同的悬停行为。

我曾考虑过使用具有一列的实际表格,但我无法仅获得标题来填充我的整个复合材料。这是我一直在使用的代码:

package widgets;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class TableHeaderButton extends Composite {

    public TableHeaderButton(Composite parent) {
        super(parent, SWT.NONE);
        this.setLayout(new GridLayout(1, true));
        makeTable(this);
    }

    private static TableViewer makeTable(Composite parent) {
        TableViewer viewer = new TableViewer(parent, SWT.FULL_SELECTION
                | SWT.H_SCROLL | SWT.V_SCROLL | SWT.NONE);

        viewer.setContentProvider(ArrayContentProvider.getInstance());

        viewer.getTable().setLayoutData(
                new GridData(SWT.FILL, SWT.FILL, false, false));

        TableViewerColumn col = new TableViewerColumn(viewer, SWT.NONE);
        col.getColumn().setWidth(100);
        col.getColumn().setText("Text Column");

        viewer.getTable().setHeaderVisible(true);

        return viewer;
    }

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

        new TableHeaderButton(shell);

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

}

任何帮助,将不胜感激。

4

2 回答 2

2

您是否尝试使用 TableColumnLayout ?

  TableColumnLayout tableColumnLayout = new TableColumnLayout();
  yourcomposite.setLayout(tableColumnLayout);
  tableColumnLayout.setColumnData(youcolumn, new ColumnWeightData(100, true));

有关更多信息,请查看:

官方文档

TableLayout 的使用示例

于 2012-11-21T10:59:22.303 回答
1

好的,我已经设法创建了一个带有单列的表并仅显示组件中的标题,我的问题是表为水平滚动条保留了空间,这是我不想要的。我仍然无法动态更改文本,很可能有一种更清洁的方法:(PS我知道内存泄漏)

package widgets;

import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public class TableHeaderButton extends Composite implements MouseListener {

    private Color highlight = new Color(getDisplay(), 255, 0, 0);
    private boolean selected;
    private TableColumn col;
    private Table table;
    private Composite tableComp;
    private GridData data;

    public TableHeaderButton(Composite parent) {
        super(parent, SWT.NONE);

        GridLayout layout = new GridLayout(2, false);
        this.setLayout(layout);

        Label label = new Label(this,SWT.NONE);
        label.setText("Label:");
        label.setLayoutData(new GridData(SWT.BEGINNING,SWT.CENTER,false,false));
        label.addMouseListener(this);
        tableComp = new Composite(this, SWT.NONE);
        data = new GridData(SWT.FILL, SWT.FILL, false, false);

        tableComp.setLayoutData(data);

        table = new Table(tableComp, SWT.NONE);


        col = new TableColumn(table, SWT.NONE);
        col.setResizable(false);
        col.setMoveable(false);
        col.addSelectionListener( new SelectionListener() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                TableHeaderButton.this.toggleSelect();
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e) {

            }
        });

        table.setHeaderVisible(true);


        TableColumnLayout tableColumnLayout = new TableColumnLayout();
        tableColumnLayout.setColumnData(col, new ColumnWeightData(100));
        tableComp.setLayout(tableColumnLayout);
        table.addMouseListener(this);
        this.setText("Text Column");

        this.addMouseListener(this);
        tableComp.addMouseListener(this);
        this.addPaintListener(new PaintListener() {

            @Override
            public void paintControl(PaintEvent e) {
                if (selected) {
                    Rectangle boounds = TableHeaderButton.this.getBounds();
                    Rectangle r = new Rectangle(0, 0, boounds.width - 1,
                            boounds.height - 1);
                    e.gc.setForeground(highlight);
                    e.gc.drawRectangle(r);
                    r = new Rectangle(1, 1, boounds.width - 3, boounds.height - 3);
                    e.gc.drawRectangle(r);
                }
                System.out.println(e.width + " " + e.height);
            }
        });


    }

    public void setText(String text) {
        col.setText(text);
        col.pack();
        table.pack();
        data.heightHint = table.getBounds().height - table.getHorizontalBar().getSize().y;
        data.widthHint = col.getWidth();
        tableComp.pack();   
        this.pack();
        this.getParent().pack();

    }

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

        TableHeaderButton b = new TableHeaderButton(shell);
        b.setLayoutData(new GridData(SWT.FILL, SWT.FILL,false, false));
        b = new TableHeaderButton(shell);
        b.setLayoutData(new GridData(SWT.FILL, SWT.FILL,false, false));


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

    @Override
    public void dispose() {
        super.dispose();
        this.highlight.dispose();
    };
    @Override
    public void mouseDoubleClick(MouseEvent e) {
        // Do nothing;

    }

    @Override
    public void mouseDown(MouseEvent e) {
        toggleSelect();
    }

    private void toggleSelect() {
        setText("Supercalifragilistic expialidocious");
        selected=!selected;
        redraw();
    }

    @Override
    public void mouseUp(MouseEvent e) {

    }

}
于 2012-11-21T15:13:32.523 回答