gpt4 book ai didi

java - 将删除按钮添加到表中的列

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:28:28 24 4
gpt4 key购买 nike

是否可以向表格中的单元格添加删除按钮?

我有一个包含 5 列的表格,我想添加第 6 列。我希望第 6 列的每一行都有一个删除按钮。

示例行:

| 10002 | part | Metal | 001 | Yes | Remove |

这样,用户只需单击按钮即可删除任何不需要的行。

我在表中有一个标记列,它是 ComboBox。我创建了一个扩展 EditingSupport 的类。

我是否需要创建另一个扩展 EditingSupport 的类,但创建一个按钮而不是 ComboBox

编辑

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

编辑

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

最佳答案

这里是示例工作版本。

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

}


}

关于java - 将删除按钮添加到表中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12480402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com