如何创建外观和行为类似于单个表头的SWT组件

时间:2012-11-21 09:44:11

标签: java swt jface

我正在尝试找到一种方法来创建一个看起来像本机表头组件的SWT组件,所以在Windows 7中:

Windows 7 Column Header

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

我已经考虑过使用一个包含一列的实际表格,但是我还没有能够只获得标题来填充整个复合词。这是我一直在使用的代码:

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();
    }

}

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:2)

您是否尝试使用TableColumnLayout?

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

有关更多信息,请查看:

Official doc

An example of use to TableLayout

答案 1 :(得分: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) {

    }

}