展开全部
我来试试:
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
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;
import org.eclipse.swt.widgets.TableItem;
public class Test {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setSize(450, 300);
shell.setText("Test");
final Label label = new Label(shell, SWT.NONE);
label.setBounds(50, 178, 311, 60);
TableViewer tableViewer = new TableViewer(shell, SWT.BORDER
| SWT.FULL_SELECTION);
final Table table = tableViewer.getTable();
table.setHeaderVisible(true);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseDoubleClick(MouseEvent e) {
int rowIndex = table.getSelectionIndex();
int columnIndex = -1;
int left = 0;
for (int i = 0; i < table.getColumns().length; i++) {
left += table.getColumns()[i].getWidth();
if (e.x < left) {
columnIndex = i;
break;
}
}
if(columnIndex != -1) {
String text = table.getItem(rowIndex).getText(columnIndex);
label.setText("double click on: "+ text);
Shell shell = new Shell();
MessageDialog.openInformation(shell, "信息", "双击单元格:"+text);
}
}
});
table.setBounds(34, 25, 350, 118);
TableColumn tableColumn = new TableColumn(table, SWT.NONE);
tableColumn.setWidth(100);
tableColumn.setText("Column One");
tableColumn = new TableColumn(table, SWT.NONE);
tableColumn.setWidth(100);
tableColumn.setText("Column Two");
tableColumn = new TableColumn(table, SWT.NONE);
tableColumn.setWidth(100);
tableColumn.setText("Column Three");
TableItem tableItem = new TableItem(table, SWT.NONE);
tableItem.setText(new String[] { "1.1", "1.2", "1.3" });
tableItem = new TableItem(table, SWT.NONE);
tableItem.setText(new String[] { "2.1", "2.2", "2.3" });
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
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;
import org.eclipse.swt.widgets.TableItem;
public class Test {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setSize(450, 300);
shell.setText("Test");
final Label label = new Label(shell, SWT.NONE);
label.setBounds(50, 178, 311, 60);
TableViewer tableViewer = new TableViewer(shell, SWT.BORDER
| SWT.FULL_SELECTION);
final Table table = tableViewer.getTable();
table.setHeaderVisible(true);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseDoubleClick(MouseEvent e) {
int rowIndex = table.getSelectionIndex();
int columnIndex = -1;
int left = 0;
for (int i = 0; i < table.getColumns().length; i++) {
left += table.getColumns()[i].getWidth();
if (e.x < left) {
columnIndex = i;
break;
}
}
if(columnIndex != -1) {
String text = table.getItem(rowIndex).getText(columnIndex);
label.setText("double click on: "+ text);
Shell shell = new Shell();
MessageDialog.openInformation(shell, "信息", "双击单元格:"+text);
}
}
});
table.setBounds(34, 25, 350, 118);
TableColumn tableColumn = new TableColumn(table, SWT.NONE);
tableColumn.setWidth(100);
tableColumn.setText("Column One");
tableColumn = new TableColumn(table, SWT.NONE);
tableColumn.setWidth(100);
tableColumn.setText("Column Two");
tableColumn = new TableColumn(table, SWT.NONE);
tableColumn.setWidth(100);
tableColumn.setText("Column Three");
TableItem tableItem = new TableItem(table, SWT.NONE);
tableItem.setText(new String[] { "1.1", "1.2", "1.3" });
tableItem = new TableItem(table, SWT.NONE);
tableItem.setText(new String[] { "2.1", "2.2", "2.3" });
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询