0

I'm working on a FXML app and I have ran into this error I can't see to resolve: The TreeViews produced by the attached code do not display their roots, even though to my best knowledge they should(the TreeViews themselves do show). Buttons and labels work fine.

On the off note, what exactly is the initialize() method supposed to do? Currently, I just write stuff in there sort of like they did in a tutorial and hope it shows up somehow. All I found about that method was the documentation on the Initializable interface, which didn't make me much wiser. Something about location and resources, but what exactly does that means in terms of Java? Those terms are too general to google for.

Also, the code is copied over from an earlier app that used Javafx only, with no FXML, and it wored fine there. So why doesn't it work now?

    package sample;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

import java.io.IOException;

public class FolderSyncerMainWindowController {

    final String FOLDER_SYNCER = "FolderSyncer";
    final String BROWSE = "Browse";
    final String SOURCE = "Source...";
    final String TARGET = "Target...";
    final String COMPARE = "Compare";
    final String CANCEL = "Cancel";
    final String SYNCHRONIZE = "Synchronize";
    final String COMPARING = "Comparing, this may take several minutes.";
    final String SYNCHRONIZING = "Synchronizing, this may take several minutes.";
    final String DONE = "Done.";


    @FXML
    private Label sourceLabel;
    @FXML
    private Label targetLabel;
    @FXML
    private Button sourceBrowseButton;
    @FXML
    private Button targetBrowseButton;
    @FXML
    private Button compareButton;
    @FXML
    private Button synchronizeButton;
    @FXML
    private TreeView sourceTreeView;
    @FXML
    private TreeView targetTreeView;
    @FXML
    private TreeItem sourceTreeViewRoot;
    @FXML
    private TreeItem targetTreeViewRoot;


    public void initialize() {
        sourceLabel.setText(SOURCE);
        targetLabel.setText(TARGET);
        sourceBrowseButton.setText(BROWSE);
        targetBrowseButton.setText(BROWSE);
        compareButton.setText(COMPARE);
        synchronizeButton.setText(SYNCHRONIZE);
        sourceTreeViewRoot = new TreeItem<>();
        targetTreeViewRoot = new TreeItem<>();
        sourceTreeViewRoot.setExpanded(true);
        targetTreeViewRoot.setExpanded(true);
        sourceTreeView = new TreeView<>(sourceTreeViewRoot);
        targetTreeView = new TreeView<>(targetTreeViewRoot);
        sourceTreeView.setShowRoot(true);
        targetTreeView.setShowRoot(true);

    }
}
Sargon1
  • 854
  • 5
  • 17
  • 48
  • ***Never*** initialize `@FXML`-annotated fields. – James_D Nov 26 '15 at 04:24
  • Why not? What do those annotations actually do? Mind you, I'm just aping what I saw in a tutorial somewhere, I never understood what those annotations, or the initialize() method, are really for. – Sargon1 Nov 26 '15 at 04:32
  • It means those are references to objects created by the `FXMLLoader`. See http://stackoverflow.com/questions/33881046/how-to-connect-fx-controller-with-main-app/33881412#33881412 – James_D Nov 26 '15 at 05:05

1 Answers1

1

There are several identical question to this on this forum, but I can't find them with a quick search.

It is always an error to initialize a @FXML-annotated field. In other words, you should never write code like

@FXML
private TreeView sourceTreeView ;

// ...

    sourceTreeView = new TreeView<>(...);

The @FXML annotation means the field refers to an object created by the FXMLLoader as part of parsing the fxml file; that object is added to the scene graph. If you then reassign the reference with sourceTreeView = new TreeView..., then the reference no longer point to the tree view that is part of the scene graph.

Instead, do

sourceTreeView.setRoot(sourceTreeViewRoot);

As an aside, you should never use raw types (TreeView) but should use an appropriate generic type: TreeView<Something>, where Something is the type of the value in each tree item.

James_D
  • 201,275
  • 16
  • 291
  • 322