私は、Java SWTのツリー構造を利用するプログラムを作成しています。これは、外部ファイルとユーザー入力から読み取られたデータを基に作成されたツリーです。 このツリーには、右クリックメニューも添付されています。Java SWTツリーメニューツリーの1つのレベルにのみ表示
主な問題は、このツリーのメニューがアイテム全体にまたがっていることです。このツリーのメニューはある程度しか表示されないようにしたいと考えています。
ツリーレベルごとに異なるメニューやメニューオプションが必要です。また
public void createTree(final Composite container){
try{
for(Object element : container.getChildren()){
if(element instanceof Tree){
((Tree) element).dispose();
}
}
// function to verify if the tree is still in my container composite, and if so, I'll dispose the tree, because I am going to construct a new one
final Tree variantTree = new Tree(container, SWT.CHECK | SWT.V_SCROLL | SWT.H_SCROLL);//creating new tree object
variantTree.setBounds(10, 65, 400, 400);//dimensions
//variantTree.setData("variant"); -- wanted to abuse this method to make a difference between the tree's level,
//but this attribute is shared across all the tree, and I cannot make a distinction between my levels
if(..){
//here I am using the data saved in a tree data structure to populate my SWT Tree
}
variantTree.addListener(SWT.Selection, new Listener(){
//this is a listener used for the tree's checkboxes, using them to select different items from my tree
});
final Menu treeMenu = new Menu(variantTree);
variantTree.setMenu(treeMenu);//adding the right click menu
treeMenu.addMenuListener(new MenuAdapter(){
//the menu's items are declared here: the buttons that I want to have and the their listeners
//i am disposing the menu's items if there are any
MenuItem newItem = new MenuItem(treeMenu, SWT.NONE);
newItem.setText("new button... ");
newItem.addListener(SWT.Selection, new Listener(){
@Override
public void handleEvent(Event event) {
String item = variantTree.getSelection()[0].getText();//can I use this to make different menus for different tree levels?
for(Node element : TestControl.getTree().getChildren()){//getting the tree data structure that I have saved in a static way via a class
if(element.getName().equals(item)){
//opening a file dialog here and doing some operations
}
}
});
});
//this is where I want to make the distinction between the levels of the tree, such as that I will have this menu show up only for the root of my tree, or the first level of the tree, since I will have multiple trees
variantTree.addMenuDetectListener(new MenuDetectListener(){
@Override
public void menuDetected(MenuDetectEvent event) {
// TODO Auto-generated method stub
if(/*condition*/){ //I messed around with the tree's methods and attributes, nothing so far has come in handy to help me make a distinction between my tree's levels
event.doit = true;
//System.out.println(variantTree.getSelectionCount()); --I don't know if this can help me either, I tried to see if this will return differently for every level of the tree, but the return values is always 0
//find some kind of handle
}else{
event.doit = false;
}
}
});
が、私は一種の中で修正さString item = variantTree.getSelection()[0].getText();
を、使用することができ、ツリーの第二のレベルのために表示されている別のメニューを持つようにしたい:
すべてのツリーの作成は、関数であります私のニーズにマッチする方法?たとえば、第2レベルのString item = variantTree.getSelection()[1].getText();
としましょう。
私のSWTツリーは、各ルートのための3の最大の深さを持つことになりますし、それはこのようなものになります。
ROOT1 - だけでこのアイテム
ためのメニューが欲しい| ---子 - レベル1
を| ------子 - レベル2
root2 - だけでこのアイテム
ためのメニューが欲しい| - | -------子 - レベル2
---子供
ありがとうou。
のためノーメニューごとに異なるメニューが表示されますか?それは私が作るはずの機能ですか? – Justplayit94
@ Justplayit94ちょうど下にスクロールすると、コードはすべてそこにあります。 – Baz
これは動作します、ありがとうございます。 – Justplayit94