java 向表中的列添加删除按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12480402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a remove button to a column in a table
提问by jkteater
Is it possible to add a Removebutton to a cell in a table?
是否可以向表格中的单元格添加删除按钮?
I have a table with 5 columns, I would like to add a 6th column. I want the 6th column to have a remove button in each row.
我有一个有 5 列的表格,我想添加第 6 列。我希望第 6 列的每一行都有一个删除按钮。
Example Row:
示例行:
| 10002 | part | Metal | 001 | Yes | Remove|
| 10002 | 部分 | 金属 | 001 | 是 | Remove|
That way the user can remove any unwanted rows by just clicking the button.
这样,用户只需单击按钮即可删除任何不需要的行。
I have a markup column in the table and it is ComboBox
. I created a class that extends EditingSupport
.
我在表中有一个标记列,它是ComboBox
. 我创建了一个扩展EditingSupport
.
Would I need to make a another class extending EditingSupport
, but creating a button instead of ComboBox
?
我是否需要创建另一个类扩展EditingSupport
,但创建一个按钮而不是ComboBox
?
EDIT
编辑
public class AplotDataTableViewer extends TableViewer
{
public AplotDataTableViewer(Composite parent, int style)
{
super(parent, style);
Table table = getTable();
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
table.setLayoutData(gridData);
createColumns();
table.setHeaderVisible(true);
table.setLinesVisible(true);
setContentProvider(new ArrayContentProvider());
}
private void createColumns() {
String[] titles = { "ItemId", "RevId", "PRL", "Dataset Name", "EC Markup" };
int[] bounds = { 150, 150, 100, 150, 100 };
TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
col.setLabelProvider(new ColumnLabelProvider() {
public String getText(Object element) {
if(element instanceof AplotDataModel.AplotDatasetData)
return ((AplotDataModel.AplotDatasetData)element).getDataset().toString();
return super.getText(element);
}
});
col = createTableViewerColumn(titles[1], bounds[1], 1);
col.setLabelProvider(new ColumnLabelProvider() {
public String getText(Object element) {
if(element instanceof AplotDataModel.AplotDatasetData)
return ((AplotDataModel.AplotDatasetData)element).getRev().toString();
return super.getText(element);
}
});
col = createTableViewerColumn(titles[2], bounds[2], 2);
col.setLabelProvider(new ColumnLabelProvider() {
public String getText(Object element) {
if(element instanceof AplotDataModel.AplotDatasetData)
return ((AplotDataModel.AplotDatasetData)element).getPRLValue().toString();
return super.getText(element);
}
});
col = createTableViewerColumn(titles[3], bounds[3], 3);
col.setLabelProvider(new ColumnLabelProvider() {
public String getText(Object element) {
if(element instanceof AplotDataModel.AplotDatasetData)
return ((AplotDataModel.AplotDatasetData)element).getDatasetName().toString();
return super.getText(element);
}
});
col = createTableViewerColumn(titles[4], bounds[4], 4);
col.setLabelProvider(new ColumnLabelProvider() {
public String getText(Object element) {
if(element instanceof AplotDataModel.AplotDatasetData)
return ((AplotDataModel.AplotDatasetData)element).getMarkupValue();
return super.getText(element);
}
});
col.setEditingSupport(new OptionEditingSupport(this));
}
private TableViewerColumn createTableViewerColumn(String header, int width, int idx)
{
TableViewerColumn column = new TableViewerColumn(this, SWT.LEFT, idx);
column.getColumn().setText(header);
column.getColumn().setWidth(width);
column.getColumn().setResizable(true);
column.getColumn().setMoveable(true);
return column;
}
}
EDIT
编辑
col = createTableViewerColumn(titles[5], bounds[5], 5);
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public void update(ViewerCell cell) {
TableItem item = new TableItem(getTable(),SWT.NONE);
Button button = new Button(getTable(),SWT.NONE);
button.setText("X");
getControl().setBackground(item.getBackground());
TableEditor editor = new TableEditor(getTable());
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(button , item, columnIndex);
editor.layout();
}
});
回答by sambi reddy
Here is sample working version.
这是示例工作版本。
public class TableEditorTest {
/**
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
TableViewer viewer = new TableViewer(shell);
viewer.getTable().setHeaderVisible(true);
viewer.getTable().setLinesVisible(true);
viewer.setContentProvider(new ArrayContentProvider());
TableColumn column = new TableColumn(viewer.getTable(), SWT.NONE);
column.setText("First Name");
column.setWidth(100);
TableViewerColumn firstNameCol = new TableViewerColumn(viewer, column);
firstNameCol.setLabelProvider(new ColumnLabelProvider(){
@Override
public String getText(Object element) {
Person p = (Person)element;
return p.getFirstName();
}
});
column = new TableColumn(viewer.getTable(), SWT.NONE);
column.setText("Last Name");
column.setWidth(100);
TableViewerColumn lastNameCol = new TableViewerColumn(viewer, column);
lastNameCol.setLabelProvider(new ColumnLabelProvider(){
@Override
public String getText(Object element) {
Person p = (Person)element;
return p.getLastName();
}
});
column = new TableColumn(viewer.getTable(), SWT.NONE);
column.setText("Actions");
column.setWidth(100);
TableViewerColumn actionsNameCol = new TableViewerColumn(viewer, column);
actionsNameCol.setLabelProvider(new ColumnLabelProvider(){
//make sure you dispose these buttons when viewer input changes
Map<Object, Button> buttons = new HashMap<Object, Button>();
@Override
public void update(ViewerCell cell) {
TableItem item = (TableItem) cell.getItem();
Button button;
if(buttons.containsKey(cell.getElement()))
{
button = buttons.get(cell.getElement());
}
else
{
button = new Button((Composite) cell.getViewerRow().getControl(),SWT.NONE);
button.setText("Remove");
buttons.put(cell.getElement(), button);
}
TableEditor editor = new TableEditor(item.getParent());
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(button , item, cell.getColumnIndex());
editor.layout();
}
});
Person p1 = new Person();
p1.setFirstName("George");
p1.setLastName("Burne");
Person p2 = new Person();
p2.setFirstName("Adam");
p2.setLastName("Silva");
Person p3 = new Person();
p3.setFirstName("Nathan");
p3.setLastName("Cowl");
List<Person> persons = new ArrayList<Person>();
persons.add(p1);
persons.add(p2);
persons.add(p3);
viewer.setInput(persons);
shell.open();
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
private static class Person
{
String firstName;
String lastName;
Person()
{
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
}
回答by simo.3792
In addition to the answer from @sambi.reddy there is a comment that says - "make sure you dispose these buttons when viewer input changes". This is what I had to do to get that part working.
除了@sambi.reddy 的回答之外,还有一条评论说-“确保在查看者输入更改时处理这些按钮”。这是我必须做的才能使那部分工作。
The framework I used had an implementation of IStructuredContentProvider, so in the overridden inputChanged(...), i had to put in the following code:-
我使用的框架有一个 IStructuredContentProvider 的实现,所以在重写的 inputChanged(...) 中,我必须输入以下代码:-
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// This will dispose of all the control button that were created previously
if (((TableViewer)viewer).getTable() != null && ((TableViewer)viewer).getTable().getChildren() != null) {
for (Control item : ((TableViewer)viewer).getTable().getChildren()) {
// at this point there are no other controls embedded in the viewer, however different instances may require more checking of the controls here.
if ((item != null) && (!item.isDisposed())) {
item.dispose();
}
}
}
}
This works fine, except if you have a refresh table (ie. viewer.setInput(List)) call as the result of you button action. When I added this the following line sometimes returned buttons that still existed, but were being disposed:
这工作正常,除非您有一个刷新表(即 viewer.setInput(List))调用作为您按钮操作的结果。当我添加此内容时,以下行有时会返回仍然存在但已被处理的按钮:
if(buttons.containsKey(cell.getElement()))
So I needed to update this line to be:
所以我需要将此行更新为:
if (buttons.containsKey(cell.getElement()) && !buttons.get(cell.getElement()).isDisposed())
Which resulted in any disposing buttons being recreated, if they were still required.
这会导致重新创建任何处置按钮,如果它们仍然需要的话。
回答by Dominik K
One important thing to the chosen answer is that you should not create an instance of TableEditor on every update method call it will slow down the performance or even make the application unresposnive.
所选答案的一个重要事项是,您不应在每次更新方法调用时创建 TableEditor 的实例,否则会降低性能甚至使应用程序无响应。
@Override
public void update(ViewerCell cell) {
TableItem item = (TableItem) cell.getItem();
Button button;
if(buttons.containsKey(cell.getElement()))
{
button = buttons.get(cell.getElement());
}
else
{
button = new Button((Composite) cell.getViewerRow().getControl(),SWT.NONE);
button.setText("Remove");
buttons.put(cell.getElement(), button);
TableEditor editor = new TableEditor(item.getParent());
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(button , item, cell.getColumnIndex());
editor.layout();
}
}
回答by sambi reddy
Below code should work for you
下面的代码应该适合你
TableItem item = (TableItem) item;
Button button = new Button(table,swt.none);
button.setText("Remove");
control.setBackground(item.getBackground());
TableEditor editor = new TableEditor(table);
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(button , item, columnIndex);
editor.layout();