问题描述
我使用 SWT 创建了一个表格,并使用 TableEditor 在表格单元格中包含了 SWT 小部件,例如标签、文本.但现在我的问题是,表格单元格的轮廓现在不可见.我想让列和行的所有边框都可见.怎么做?设置 table.setLinesVisible(true) 不成功!
I created a table with SWT and I included SWT widgets such as Label, Text in the table cells using TableEditor. But now my problem is, the outlines of the table cells are not visible now. I want to make visible all the borders of columns and rows. How to do it? Setting table.setLinesVisible(true) is not a success!
Table table = new Table(detailArea, SWT.BORDER | SWT.MULTI); table.setLinesVisible(true); for (int i = 0; i < 4; i++) { TableColumn column = new TableColumn(table, SWT.NONE); column.setWidth(100); } TableItem x1= new TableItem(table, SWT.NONE); TableEditor editorw = new TableEditor(table); Label lblName =new Label(table, SWT.NONE); lblName.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); lblName.setText("Language"); editorw.grabHorizontal = true; editorw.setEditor(lblName, x1, 0); editorw = new TableEditor(table); Text txtLang = new Text(table, SWT.BORDER); editorw.grabHorizontal = true; editorw.setEditor(txtLang, x1, 1); editorw = new TableEditor(table); Label lblReference = new Label(table, SWT.NONE); lblReference.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); lblReference.setText("Value"); editorw.grabHorizontal = true; editorw.setEditor(lblReference, x1, 2); editorw = new TableEditor(table); Text txtValue= new Text(table, SWT.BORDER); editorw.grabHorizontal = true; editorw.setEditor(txtValue, x1, 3); /////table row 2 TableItem x2= new TableItem(table, SWT.NONE); editorw = new TableEditor(table); Label lblName2 =new Label(table, SWT.NONE); lblName2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); lblName2.setText("Language"); editorw.grabHorizontal = true; editorw.setEditor(lblName2, x2, 0); editorw = new TableEditor(table); Text txtLang2 = new Text(table, SWT.BORDER); editorw.grabHorizontal = true; editorw.setEditor(txtLang2, x2, 1); editorw = new TableEditor(table); Label lblReference2 = new Label(table, SWT.NONE); lblReference2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); lblReference2.setText("Value"); editorw.grabHorizontal = true; editorw.setEditor(lblReference2, x2, 2); editorw = new TableEditor(table); Text txtValue2= new Text(table, SWT.BORDER); editorw.grabHorizontal = true; editorw.setEditor(txtValue2, x2, 3); table.setLinesVisible(true);
以下是当前输出:
推荐答案
您可以将编辑器小部件(标签和文本)包装到 Composite 中.可以给复合材料一个边框和一个可以指定边距的布局:
You can wrap the editor widgets (label and text) into a Composite. The composite can be given a border and a layout that can specify a margin:
Composite composite = new Composite( table, SWT.BORDER ); GridLayout layout = new GridLayout(); composite.setLayout( layout ); Label label = new Label( composite, SWT.NONE ); GridData gridData = new GridData( SWT.FILL, SWT.FILL, true, true ); gridData.verticalIndent = 2; gridData.horizontalIndent = 2; label.setLayoutData( gridData ); editor.setEditor( composite, item, 0 );
另一种方法可能是 所有者绘制表格项 特别是查看可用于绘制背景的 EraseItem 事件.不过恐怕你不能影响 TableEditor 的布局,自绘边框会被编辑器部件隐藏.
A different approach might be owner draw table items In particular see the EraseItem event that can be used to draw the background. However I am afraid that since you cannot influence the TableEditor's layout, the owner-drawn borders will be hidden by the editor widgets.
请注意,这种方法(无论是您的初始方法还是复合包装的方法)都不能很好地扩展到数百行.对于每一行,将创建 4 或 8 个小部件.根据客户端计算机的性能,它会在创建数百个小部件时变慢并最终耗尽句柄.
Note that this approach (neither your initial nor the composite-wrapped one) will not scale well for several hundret rows. For each row, 4 or 8 widgets will be created. Depending on the client computers performance it will slow down when creating several hundret widgets and eventually run out of handles.