0

JavaFXの問題というよりジェネリックの使い方の問題なのかもしれませんが、エラーの解決方法がわかりません.すみませんがご教示お願いいたします.

背景

JavaFXを使用してデスクトップアプリを作ろうとしています.TreeViewを表示させたいのですが、初期化の箇所でコンパイルエラーになってしまいます.

開発環境
jdk 17.0.1
JavaFx 17.0.2
Eclipse 2021-12

エラーメッセージ

Description Resource    Path    Location    Type
The method setRoot(TreeItem<TreeItem<TreeItemDataDitaOtTree>>) 
in the type TreeView<TreeItem<TreeItemDataDitaOtTree>> 
is not applicable for the arguments (TreeItem<TreeItemDataDitaOtTree>)  
OtMainController.java   /OtInvoker/src/com/xxx/ditaot/app   line 88 Java Problem

コード

FXML

OtMain.fxml(TreeView該当箇所)

    <TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab text="%tabTxtDitaOt">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="346.0" prefWidth="200.0">
                 <children>
                     <TreeView prefHeight="346.0" prefWidth="200.0" fx:id="tvDitaOt"/>
                 </children></AnchorPane>
            </content>
          </Tab>

コントローラ

OtMainController.java(関係ないコードもあると思います)

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import com.xxx.ditaot.util.XmlResourceBundle;
import com.xxx.ditaot.util.XmlResourceBundleControl;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;

//...
public class OtMainController  extends Application implements Initializable{
    private static final String RESOURCE_BUNDLE_PATH = "." + File.separator + "resource" + File.separator + "property";
    private static final String ICON_FILE_PATH = "." + File.separator + "resource" + File.separator + "icon";
    private static final String DITAOT_ROOT_ICON = "icons8-home-48.png";
    private static final String DITAOT_ICON = "icons8-folder-48.png";
    private static final String DITAOT_PLUGIN_ICON = "icons8-file-check-48.png";
    private Scene scene;
    private Pane root;
    static  Stage stage;
    private String otDirPath = null;
    private ResourceBundle.Control control;
    private ResourceBundle bundle;
    private final Node iconDitaOtRoot;
    private final Node iconDitaOt;
    private final Node iconDitaOtPlugin;

    public OtMainController() throws MalformedURLException {
        ditaOtInfos = new DitaOtInfos();
        control = new XmlResourceBundleControl();
        URLClassLoader urlLoader = new URLClassLoader(new URL[]{new File(RESOURCE_BUNDLE_PATH).toURI().toURL()});
        Locale locale=Locale.getDefault();
        bundle = XmlResourceBundle.getBundle("ditaOtAppProp", locale, urlLoader, control);
        iconDitaOtRoot = new ImageView(new File(ICON_FILE_PATH + File.separator + DITAOT_ROOT_ICON).toURI().toString() );
        iconDitaOt = new ImageView(new File(ICON_FILE_PATH + File.separator + DITAOT_ICON).toURI().toString() );
        iconDitaOtPlugin = new ImageView(new File(ICON_FILE_PATH + File.separator + DITAOT_PLUGIN_ICON).toURI().toString() );
        
    }
    
    @FXML
    private TreeView<TreeItem<TreeItemDataDitaOtTree>> tvDitaOt;
    public void setScene(Scene scene) {
        this.scene = scene;
    }
    public void setRoot(Pane root) {
        this.root = root;
    }
    @Override
    public void initialize(java.net.URL url, java.util.ResourceBundle bundle) {
        TreeItemDataDitaOtRoot dataDitaOtRoot = new TreeItemDataDitaOtRoot(bundle.getString("tvTxtDitaOtRoot"));
        TreeItem<TreeItemDataDitaOtTree> ditaOtTreeItemRoot = new TreeItem<TreeItemDataDitaOtTree>(dataDitaOtRoot, iconDitaOtRoot);

        this.tvDitaOt.setRoot(ditaOtTreeItemRoot); // ここでコンパイルエラー
        this.tvDitaOt.setShowRoot(false);
    }
    
    public static void setStage(Stage stageParam) {
        stage = stageParam;
    }
    //...
}

関連するクラス

TreeItemDataDitaOtTree.java

public abstract class TreeItemDataDitaOtTree {
    // Empty!
}

TreeItemDataDitaOtRoot.java

public class TreeItemDataDitaOtRoot extends TreeItemDataDitaOtTree{
    private final String rootTitle;
    
    public String getRootTitle() {
        return this.rootTitle;
    }

    public TreeItemDataDitaOtRoot(String rootTitle) {
        this.rootTitle = rootTitle;
    }
    
    public String toString() {
        return getRootTitle();
    }
}

1 件の回答 1

0

自己解決しました.

setRoot

The method setRoot(TreeItem<TreeItem>) in the
type TreeView<TreeItem>

のように解釈されるのはどうにもわかりません.

public final void setRoot​(TreeItem value)

のはずです.

https://openjfx.io/javadoc/17/javafx.controls/javafx/scene/control/TreeView.html#setRoot(javafx.scene.control.TreeItem)

解決法は、ただひたすらに @SuppressWarnings で逃げるのみでした.以下でコンパイルエラーは消えます.

    @FXML
    @SuppressWarnings({"rawtypes"})
    private TreeView tvDitaOt;
    @Override
    @SuppressWarnings({"unchecked"})
    public void initialize(java.net.URL url, java.util.ResourceBundle bundle) {
        TreeItemDataDitaOtRoot dataDitaOtRoot = new TreeItemDataDitaOtRoot(bundle.getString("tvTxtDitaOtRoot"));
        TreeItem<TreeItemDataDitaOtTree> ditaOtTreeItemRoot = new TreeItem<TreeItemDataDitaOtTree>(dataDitaOtRoot, iconDitaOtRoot);
        this.tvDitaOt.setRoot(ditaOtTreeItemRoot);
        this.tvDitaOt.setShowRoot(false);
    }

この後でTreeViewを更新するコードを動かすと(まだボコボコではありますが)表示してくれました.なんとかこの先コーディングを進められそうです.

TreeViewの表示

お騒がせいたしました.

追記

違っていました.TreeViewの型パラメータの使い方をまったくわかっていませんでした.@SuppressWarnings は必要ありません.

    @FXML
    private TreeView<TreeItemDataDitaOtTree> tvDitaOt;
    @Override
    public void initialize(java.net.URL url, java.util.ResourceBundle bundle) {
        TreeItemDataDitaOtRoot dataDitaOtRoot = new TreeItemDataDitaOtRoot(bundle.getString("tvTxtDitaOtRoot"));
        TreeItem<TreeItemDataDitaOtTree> ditaOtTreeItemRoot = new TreeItem<TreeItemDataDitaOtTree>(dataDitaOtRoot);

        this.tvDitaOt.setRoot(ditaOtTreeItemRoot);
        this.tvDitaOt.setShowRoot(false);
    }

が正解です.

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。