我的表格的最後一列填充了一個按鈕。當用戶點擊按鈕時,我想打開一個與按鈕位於同一行的文件。查找表格單元格中單擊按鈕的行索引
下面是按鈕的列代碼。
case 6:
Map<Object, Button> buttons = new HashMap<Object, Button>();
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.PUSH);
//button.setText("Adobe");
button.setImage(loadImage("/images/AdobeIcon.png"));
buttons.put(cell.getElement(), button);
}
TableEditor editor = new TableEditor(item.getParent());
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(button , item, cell.getColumnIndex());
button.addListener(SWT.Selection, new SelectionListener(item, button, cell.getElement()));
editor.layout();
是否有可能選擇的索引發送到監聽器類? 如果他們點擊第一個按鈕 - 發送0 ,如果他們點擊第二個按鈕 - 發送1
我是否甚至需要TableEditor?
如果這是可能的話,它會使偵聽器代碼變得簡單。
這就是我正在嘗試偵聽器代碼。
@Override
public void handleEvent(Event event) {
AplotSaveDataModel.SaveData selected = ((AplotSaveDataModel.SaveData)element);
int index = AplotSaveDataModel.getInstance().getIndexOf(selected);
final File viewerFile = new File(AplotSaveDataModel.getInstance().getSelectedPDFFileName(index));
try {
Desktop.getDesktop().open(viewerFile);
}
catch (IOException e) {
e.printStackTrace();
}
}
- 爲什麼上面總是返回0索引的代碼?
編輯
我不完全理解如何讓按鈕的數據行索引。
button.setData("row.id", rowId);
我假設「row.id」是一個鍵,rowId是值。所以我需要用代碼填充rowId來獲得行索引。
int rowId = "Some code to get the Row Index"?
如果我想獲得的列索引,我可以做
int rowId = cell.getColumnIndex();
我無法弄清楚如何填充ROWID才能夠獲得行索引/號碼是多少?
下面是代碼來獲取列號(不需要)
case 6:
Map<Object, Button> buttons = new HashMap<Object, Button>();
TableItem item = (TableItem) cell.getItem();
Button button;
***int rowId = cell.getColumnIndex();***
if(buttons.containsKey(cell.getElement())) {
button = buttons.get(cell.getElement());
}
else
{
button = new Button((Composite) cell.getViewerRow().getControl(),SWT.PUSH);
//button.setText("Adobe");
button.setImage(loadImage("/images/AdobeIcon.png"));
***button.setData("row.id", rowId);***
buttons.put(cell.getElement(), button);
}
TableEditor editor = new TableEditor(item.getParent());
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(button , item, cell.getColumnIndex());
button.addListener(SWT.Selection, new SelectionListener(item, button, cell.getElement()));
editor.layout();
}
監聽器類
@Override
public void handleEvent(Event event) {
if (event.widget instanceof Button) {
int rowId = (Integer) event.widget.getData("row.id");
System.out.println("Row Int " + rowId + "\n");
}
請幫我得到這個工作 - 這是一個非常重要的部分我項目
*** EDIT 2 *******
public class AplotPDFResultsTableViewer extends TableViewer {
public AplotPDFResultsTableViewer(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());
}
public static Image loadImage(String path)
{
Image newImage = null;
try {
newImage = new Image(null, AplotPDFResultsTableViewer.class.getClassLoader().getResourceAsStream(path));
}
catch(SWTException ex) {
System.out.println("Couldn't find " + path);
ex.printStackTrace();
}
return newImage;
}
private void createColumns() {
TableViewerColumn col = new TableViewerColumn(this , SWT.NONE);
col.getColumn().setWidth(150);
col.getColumn().setText("Creation Date");
col.setLabelProvider(new ColorColumnLabelProvider());
col = new TableViewerColumn(this , SWT.NONE);
col.getColumn().setWidth(150);
col.getColumn().setText("ItemId");
col.setLabelProvider(new ColorColumnLabelProvider());
col = new TableViewerColumn(this , SWT.NONE);
col.getColumn().setWidth(50);
col.getColumn().setText("RevId");
col.setLabelProvider(new ColorColumnLabelProvider());
col = new TableViewerColumn(this , SWT.NONE);
col.getColumn().setWidth(50);
col.getColumn().setText("PRL");
col.setLabelProvider(new ColorColumnLabelProvider());
col = new TableViewerColumn(this , SWT.NONE);
col.getColumn().setWidth(200);
col.getColumn().setText("File Location");
col.setLabelProvider(new ColorColumnLabelProvider());
col = new TableViewerColumn(this , SWT.NONE);
col.getColumn().setWidth(200);
col.getColumn().setText("File Name");
col.setLabelProvider(new ColorColumnLabelProvider());
col = new TableViewerColumn(this , SWT.NONE);
col.getColumn().setWidth(50);
col.getColumn().setText("");
col.setLabelProvider(new ColorColumnLabelProvider());
}
public class ColorColumnLabelProvider extends ColumnLabelProvider {
@Override
public void update(final ViewerCell cell) {
Object element = cell.getElement();
if(element instanceof AplotSaveDataModel.SaveData) {
AplotSaveDataModel.SaveData p = (AplotSaveDataModel.SaveData) element;
switch(cell.getColumnIndex()) {
case 0:
cell.setText(p.getDateTime());
break;
case 1:
cell.setText(p.getItemId());
break;
case 2:
cell.setText(p.getRevId());
break;
case 3:
cell.setText(p.getPRL());
break;
case 4:
//cell.setForeground(black);
cell.setText(p.getFileDir());
break;
case 5:
//cell.setForeground(black);
cell.setText(p.getfileName());
break;
case 6:
Map<Object, Button> buttons = new HashMap<Object, Button>();
TableItem item = (TableItem) cell.getItem();
Button button;
int rowId = cell.getColumnIndex();
if(buttons.containsKey(cell.getElement())) {
button = buttons.get(cell.getElement());
}
else
{
button = new Button((Composite) cell.getViewerRow().getControl(),SWT.PUSH);
//button.setText("Adobe");
button.setImage(loadImage("/images/AdobeIcon.png"));
button.setData("row.id", rowId);
buttons.put(cell.getElement(), button);
}
TableEditor editor = new TableEditor(item.getParent());
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(button , item, cell.getColumnIndex());
button.addListener(SWT.Selection, new SelectionListener(item, button, cell.getElement()));
editor.layout();
}
}
}
}// End ColorColumnLabelProvider Class
// ===============================================================
//
// SelectionListener Class
//
// ===============================================================
class SelectionListener implements Listener {
TableItem item;
Button editButton;
Object element;
//////////////////////////////////////////////////////////////////////////
// Constructor //
//////////////////////////////////////////////////////////////////////////
public SelectionListener(TableItem item, Button editButton, Object element) {
this.item = item;
this.editButton = editButton;
this.element = element;
}
//////////////////////////////////////////////////////////////////////////
// handleEvent //
//////////////////////////////////////////////////////////////////////////
@Override
public void handleEvent(Event event) {
if (event.widget instanceof Button) {
int rowId = (Integer) event.widget.getData("row.id");
System.out.println("Row Int " + rowId + "\n");
}
//AplotSaveDataModel.SaveData selected = ((AplotSaveDataModel.SaveData)element);
//int index = AplotSaveDataModel.getInstance().getIndexOf(selected);
//final File viewerFile = new File(AplotSaveDataModel.getInstance().getSelectedPDFFileName(index));
//try {
// Desktop.getDesktop().open(viewerFile);
//}
//catch (IOException e) {
// e.printStackTrace();
//}
}
}// End SelectionListener Class
} // End AplotPDFTableViewer Class
能請你也加入'ArrayContentProvider' –
@Waqas llyas,你的意思是其他然後setContentProvider(新ArrayContentProvider());那是在班級的頂部? – jkteater
是的,但ArrayContentProvider的完整類在哪裏?它來自API嗎? –