我认为唯一与此不同的是SceneBuilder的版本是8.3,我使用TreeTableView和TreeTableColumn与Oracle Java 8 这是fxml
<?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和控制器源的位置根据它们所在的名称空间进行调整,这反映在fxml中。
css文件是空的,因为我此时只在SceneBuilder中定义了它,这就是为什么我没有包含它(只有注释)。
有人认为我从开始使用SceneBuilder时就记得它是默认无效的倾向。 Tnanks!
答案 0 :(得分:1)
您实际上需要为要根据fxml创建的控制器加载fxml。也没必要两次通过该位置:
url = getClass().getResource("/AudioExplorer.fxml");
explorerLoader = new FXMLLoader(url);
try {
TreeTableView<?> audioExplorer = explorerLoader.load();
audioController = explorerLoader.getController();
...
} catch ...
此外,没有理由在字段中保留对FXMLLoader
实例的引用。完成加载后,只需提取您需要的信息即可。&#34; drop&#34;加载器参考。不管怎样,应该避免再次拨打load
......
答案 1 :(得分:0)
除了fabian的答案之外,请注意我已经写了一些utility methods,这可以将FXML加载减少到单个语句,例如:
TreeTableView<?> audioExplorer = FXMLLoaders.load(AudioExplorer.class);
请注意,只有遵守以下命名约定才能实现:
如果是一个班级&#34; mypackage。&lt; 名称&gt;&#34;加载FXML文件,然后加载FXML文件 应该在同一个包中并命名为&#34;&lt; 名称&gt; .fxml &#34;。
该库是开源的。也许你觉得它很有用:
<dependency>
<groupId>org.drombler.commons</groupId>
<artifactId>drombler-commons-fx-core</artifactId>
<version>0.10</version>
</dependency>
您可以在此处详细了解: http://puces-blog.blogspot.ch/2015/03/drombler-commons-conventions-to.html