我相信這個唯一不同的是SceneBuilder的版本是8.3,我使用TreeTableView和TreeTableColumn與Oracle的Java 8 這裏是FXMLjavafx8 FXMLLoader返回null
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TreeTableColumn?>
<?import javafx.scene.control.TreeTableView?>
<TreeTableView maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="1" minWidth="1" prefHeight="400.0" prefWidth="600.0" showRoot="false" stylesheets="@org/cornova/AudioExplorer.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.cornova.AudioExplorerController">
<columns>
<TreeTableColumn fx:id="mixerName" editable="false" maxWidth="6000.0" minWidth="100.0" prefWidth="-1.0" text="Mixers" />
<TreeTableColumn fx:id="lineNames" maxWidth="49.0" minWidth="100.0" prefWidth="-1.0" text="Lines" />
<TreeTableColumn fx:id="controlNames" minWidth="100.0" prefWidth="-1.0" text="Controls" />
<TreeTableColumn fx:id="formatsNames" minWidth="100.0" prefWidth="-1.0" text="Formats" />
</columns>
</TreeTableView>
這裏是控制器類
/**
* Sample Skeleton for 'AudioExplorer.fxml' Controller Class
*/
package org.cornova;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.TreeTableColumn;
public class AudioExplorerController {
@FXML // ResourceBundle that was given to the FXMLLoader
private ResourceBundle resources;
@FXML // URL location of the FXML file that was given to the FXMLLoader
private URL location;
@FXML // fx:id="mixerName"
private TreeTableColumn<?, ?> mixerName; // Value injected by FXMLLoader
@FXML // fx:id="lineNames"
private TreeTableColumn<?, ?> lineNames; // Value injected by FXMLLoader
@FXML // fx:id="controlNames"
private TreeTableColumn<?, ?> controlNames; // Value injected by FXMLLoader
@FXML // fx:id="formatsNames"
private TreeTableColumn<?, ?> formatsNames; // Value injected by FXMLLoader
@FXML // This method is called by the FXMLLoader when initialization is complete
void initialize() {
assert mixerName != null : "fx:id=\"mixerName\" was not injected: check your FXML file 'AudioExplorer.fxml'.";
assert lineNames != null : "fx:id=\"lineNames\" was not injected: check your FXML file 'AudioExplorer.fxml'.";
assert controlNames != null : "fx:id=\"controlNames\" was not injected: check your FXML file 'AudioExplorer.fxml'.";
assert formatsNames != null : "fx:id=\"formatsNames\" was not injected: check your FXML file 'AudioExplorer.fxml'.";
}
}
這裏是主類
package org.cornova;
import java.net.URL;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TreeTableColumn.CellDataFeatures;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.stage.Stage;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.Mixer.Info;
/**
*
* @author walt
*/
public class AudioExplorer extends Application {
private URL url;
private FXMLLoader explorerLoader;
private AudioExplorerController audioController;
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Audio Explorer");
final Scene scene = new Scene(new Group(), 200, 400);
Group sceneRoot = (Group)scene.getRoot();
try {
url = getClass().getResource("/AudioExplorer.fxml");
explorerLoader = new FXMLLoader(url);
explorerLoader.setLocation(url);
audioController = explorerLoader.getController();
} catch (Exception e) {
System.out.println(e);
}
//Creating the root element
TreeItem rootNode = new TreeItem();
rootNode.setExpanded(true);
Info mixers[] = AudioSystem.getMixerInfo();
rootNode = new TreeItem<>(mixers[0]);
//Creating tree items
for (int i = 0; i < mixers.length; i++) {
rootNode.getChildren().add(new TreeItem<>(mixers[i]));
}
//Creating a column
TreeTableColumn<Info,String> mixerInfo = new TreeTableColumn<>("Mixers");
mixerInfo.setMaxWidth(1500);
//Defining cell content
mixerInfo.setCellValueFactory((CellDataFeatures<Info, String> p) ->
new ReadOnlyStringWrapper(p.getValue().getValue().getName()));
//Creating a tree table view
final TreeTableView<Info> treeTableView = new TreeTableView<>(rootNode);
treeTableView.getColumns().add(mixerInfo);
treeTableView.setMaxWidth(1500);
treeTableView.setShowRoot(false);
treeTableView.autosize();
sceneRoot.getChildren().add(treeTableView);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
這都是在netbeans 8.2下完成的。我已經完成了,也許在18個月前,這些基於場景構建器的圖形應用程序可能有幾十個。我當時發現的一件事是確保css和controller源的位置針對它們所在的名稱空間進行了調整,這反映在fxml中。
css文件是空的,因爲我在此時只在SceneBuilder中定義了它,這就是爲什麼我沒有包含它(只有註釋)。
有人認爲我從SceneBuilder開始時就記得,它傾向於默默地失敗。 Tnanks!
你必須調用'FXMLLoader#load'爲裝載機創建和填充控制器。 – Itai