TreeView initialization setRoot() results in compilation error

Asked 2 years ago, Updated 2 years ago, 69 views

It may be more of a generic usage issue than a JavaFX issue, but I don't know how to resolve the error. Please let me know.

I'm trying to create a desktop app using JavaFX. I'd like to see TreeView, but I get a compilation error during initialization.

Development Environment
jdk17.0.1
JavaFx17.0.2
Eclipse 2021-12

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

FXML

OtMain.fxml (TreeView Affected)

<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>

controllers

OtMainController.java (some codes may not be relevant)

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.Initialize;
import com.xxx.ditaot.util.XmlResourceBundle;
import com.xxx.ditaot.util.XmlResourceBundleControl;

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

//...
public class OtMainController extensions 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 = "icons 8-home-48.png";
    private static final String DITAOT_ICON = "icons 8-folder-48.png";
    private static final String DITAOT_PLUGIN_ICON = "icons8-file-check-48.png";
    private Scene;
    private Pane root;
    static Stage stage;
    private String atDirPath=null;
    private ResourceBundle.Control control;
    private ResourceBundle bundle;
    private final Node iconDitaOtRoot;
    private final NodeiconDitaOt;
    private final Node iconDitaOtPlugin;

    public OtMainController()throws MalformedURLException {
        ditaOtInfos = new DitaOtInfos();
        control = new XmlResourceBundleControl();
        URLClassLoader urlLoader=newURLClassLoader(newURL[]{newFile(RESOURCE_BUNDLE_PATH).toURI().toURL()});
        Locale locale= Locale.getDefault();
        bundle=XmlResourceBundle.getBundle("ditaOtAppProp", locale, urlLoader, control);
        iconDitaOtRoot=new ImageView(newFile(ICON_FILE_PATH+File.separator+DITAOT_ROOT_ICON).toURI().toString();
        iconDitaOt=new ImageView(newFile(ICON_FILE_PATH+File.separator+DITAOT_ICON).toURI().toString();
        iconDitaOtPlugin=new ImageView(newFile(ICON_FILE_PATH+File.separator+DITAOT_PLUGIN_ICON).toURI().toString();
        
    }
    
    @ FXML
    private TreeView <TreeItem<TreeItemDataDataOtTree>tvDitaOt;
    public void setScene(SceneScene) {
        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 TreeItemDataOtRoot(bundle.getString("tvTxtDitaOtRoot"));
        TreeItem<TreeItemDataDataOtTree>ditaOtTreeItemRoot=newTreeItemDataDataOtTree>(dataDitaOtRoot, iconDitaOtRoot);

        This.tvDitaOt.setRoot(ditaOtTreeItemRoot); // Compilation error here
        This.tvDitaOt.setShowRoot(false);
    }
    
    public static void setStage(StageParam){
        stage=stageParam;
    }
    //...
}

Related Classes

TreeItemDataDitaOtTree.java

public abstract class TreeItemDataDataOtTree{
    // Empty!
}

TreeItemDataDataOtRoot.java

public class TreeItemDataDataOtRoot extensions TreeItemDataDataOtTree{
    private final String rootTitle;
    
    public String getRootTitle() {
        return this.rootTitle;
    }

    public TreeItemDataDataOtRoot(String rootTitle){
        This.rootTitle=rootTitle;
    }
    
    public String to String() {
        return getRootTitle();
    }
}

java javafx

2022-09-30 10:27

1 Answers

Self-resolved.

setRootis

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

I don't know how to interpret it as .

public final void setRoot (TreeItem value)

It should be .

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

The solution was to simply run away with @SuppressWarnings. The compilation error disappears below.

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

After this, I moved the code to update TreeView (although it's still bumpy). I think I can somehow proceed with coding.

View TreeView

Sorry for the trouble.

Wrong. I didn't know how to use TreeView type parameters at all. @SuppressWarnings is not required.

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

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

is the correct answer.


2022-09-30 10:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.