私は、テキストボックスとボタンとファイルダイアログを使用してユーザーからの入力をいくつか受け取り、それらをSWTツリーにフォーマットするプログラムを作成しました。それは要素であるチェックボックスで選択できます。Java SWTツリーTreeItemリスナーは、ウィジェットの例外例外エラーを返します
私が直面している問題は、リアルタイムでユーザーのために更新したいということです。このフォーラムでは、コンテナのlayout()
メソッドの使用に関する解決策が見つかりました。私がこのツリーのdispose()
と呼んだら、それを再構築して後で再描画します。
このツリーの背後には、すべてのデータを管理するために使用するツリーデータ構造があります。 また、私が使用しているクラスはSWTのDialog
インターフェイスを実装しているので、ボタンを押すと、ツリーとテキスト入力エリアがあるウィンドウが表示されます。
私はツリーが存在するコンテナを宣言しました。
final Composite container = new Composite(parent, SWT.NONE);
次の私は、ユーザー入力のためのボタンやテキスト領域のためのいくつかの他のコードを持っていますが、そのツリー
createTree(container);
の実際のビルドプロセスが含まれ、この静的メソッドを呼び出していますプログラムの動作やスローされた例外に影響を与えません。
これは、静的メソッドcreateTree(container);
public void createTree(final Composite container){
try{
for(Object element : container.getChildren()){
if(element instanceof Tree){
((Tree) element).dispose();
}
}//here I am disposing of the previous tree and then I'm creating a new one to be drawn in the container when the layout() method will be called
final Tree variantTree = new Tree(container, SWT.CHECK | SWT.V_SCROLL | SWT.H_SCROLL);
variantTree.setBounds(10, 65, 400, 400);
//here is where I am populating the tree with the data I have stored in the Tree Data Structure that I've mentioned
if(TestControl.getTree().getChildren().size() > 0){
for(final Node variantElement : TestControl.getTree().getChildren()){
final TreeItem variantTreeItem = new TreeItem(variantTree, 0);
variantTreeItem.setText(variantElement.getName());
variantTreeItem.setChecked(variantElement.getState());
for(Node suiteElement : variantElement.getChildren()){
TreeItem suiteTreeItem = new TreeItem(variantTreeItem, 0);
suiteTreeItem.setText(suiteElement.getName());
suiteTreeItem.setChecked(suiteElement.getState());
for(Node testElement : suiteElement.getChildren()){
TreeItem testTreeItem = new TreeItem(suiteTreeItem, 0);
testTreeItem.setText(testElement.getName());
testTreeItem.setChecked(testElement.getState());
}
}
}
}
//here is the actual problem, the exception's stack trace points to the line where my next comment is. this listener is used to bring a file dialog window where I can select a file and use it later on
variantTree.addListener(SWT.MouseDoubleClick, new Listener(){
@Override
public void handleEvent(Event event) {
try{
// TODO Auto-generated method stub
Point point = new Point(event.x, event.y);
if(!point.equals(null)){
TreeItem item = variantTree.getItem(point);
for(Node element : TestControl.getTree().getChildren()){
if(element.getName().equals(item.getText())){//here is the problem, why is it trying to tell me
FileDialog fileDialog = new FileDialog(container.getParent().getShell(), SWT.OPEN);
//filtering for extensions
//filtering for path
String path;
if((path = fileDialog.open()) != null){
String fileName = fileDialog.getFileName();
Node suiteNode = new Node(element);
suiteNode.setName(fileName);
TestControl.addChild(suiteNode);
createTree(container);
//here I call the method in a recursive way. After I modified my Tree Data Structure with the data I got from the user, I want to redraw the tree in a real timed fashion
}
}
}
}
}catch(Exception exception){
exception.printStackTrace();
Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, exception.getLocalizedMessage(), exception);
ErrorDialog.openError(null, "Error", "Error occured!", status);
}
}
});
variantTree.addListener(SWT.Selection, new Listener(){
@Override
public void handleEvent(Event event) {
// TODO Auto-generated method stub
if(event.detail == SWT.CHECK){
TreeItem item = (TreeItem) event.item;
for(Node element : TestControl.getTree().getChildren()){
if(element.getName().equals(item.getText())){
element.setState(item.getChecked());
}
}
for(Node element : TestControl.getTree().getChildren()){
for(Node nextElement : element.getChildren()){
if(nextElement.getName().equals(item.getText())){//here the error doesnt show up, even though I am using the SWT Tree element as above
nextElement.setState(item.getChecked());
}
}
}
for(Node element : TestControl.getTree().getChildren()){
for(Node nextElement : element.getChildren()){
for(Node lastElement : nextElement.getChildren()){
if(lastElement.getName().equals(item.getText())){
lastElement.setState(item.getChecked());
}
}
}
}
}
}
});
}catch(Exception exception){
exception.printStackTrace();
Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, exception.getLocalizedMessage(), exception);
ErrorDialog.openError(null, "Error", "Error occured!", status);
}
}
である私も、私はdispose
を呼び出していたとき、私は同様にリスナーを取り除く必要があるため、このエラーが表示される可能性があることを読みました。 これは例外の原因になる可能性がありますか? ありがとうございました。ハグコードの部分は残念です。
ツリーの子のみを削除し、同じツリーを保持できますか?うーん、私はあなたのソリューションを試してみるよ、ありがとう。 – Justplayit94
'Tree.removeAll'は、既存のツリー項目をすべて削除します。しかし、あなたはまだあなたが廃棄アイテムを参照していないことを確認する必要があります。 –
それは働いて、おかげで束。 – Justplayit94