好的,我已经设法创建了一个带有单列的表并仅显示组件中的标题,我的问题是表为水平滚动条保留了空间,这是我不想要的。我仍然无法动态更改文本,很可能有一种更清洁的方法:(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) {
}
}