Làm cách nào để thêm menu bật lên nhấp chuột phải vào cây thông trong cây SWT. Mỗi cây phải được kích chuột phải vào nóThêm menu chuột phải vào cây xanh trong cây SWT
Trả lời
Chỉ cần sử dụng tree.setMenu(Menu)
.
Có bạn đi:
public static void main(String[] args)
{
Display display = Display.getDefault();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
final Tree tree = new Tree(shell, SWT.NONE);
for(int i = 0; i < 10; i++)
{
TreeItem item = new TreeItem(tree, SWT.NONE);
item.setText("Parent " + i);
for(int j = 0; j < 3; j++)
{
TreeItem child = new TreeItem(item, SWT.NONE);
child.setText("Child " + i + " " + j);
}
}
final Menu menu = new Menu(tree);
tree.setMenu(menu);
menu.addMenuListener(new MenuAdapter()
{
public void menuShown(MenuEvent e)
{
MenuItem[] items = menu.getItems();
for (int i = 0; i < items.length; i++)
{
items[i].dispose();
}
MenuItem newItem = new MenuItem(menu, SWT.NONE);
newItem.setText("Menu for " + tree.getSelection()[0].getText());
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Nó được làm việc! Cảm ơn một tấn! – user2598071
@ user2598071 Bạn được chào đón. – Baz
Có cách nào để khóa hiển thị menu chỉ cho cấp đầu tiên của cây không? Ví dụ, để được hiển thị chỉ cho thư mục gốc. – Justplayit94
@ dic19 tôi phải sử dụng chỉ SWT.Adding người nghe đến một treeitem với sự kiện như SWT.MenuDetect không hoạt động – user2598071