0

当我尝试用我的扩展版本替换标准 TreeView 时,我遇到了这个异常,以简化我创建一个简单项目的事情。当然有一个简单的解决方案。

例外:

Can not set testtreeviewwithitems.MyTestTree field testtreeviewwithitems.Sample.treeView to javafx.scene.control.TreeView
/C:/Users/Vlad/Documents/NetBeansProjects/TestTreeViewWithITems/build/classes/testtreeviewwithitems/Sample.fxml:10
  at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
  at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
  at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
  at java.lang.reflect.Field.set(Field.java:680)
  at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:659)
  at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:572)
  at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2314)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2131)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2742)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2694)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2683)
  at testtreeviewwithitems.TestTreeViewWithITems.start(TestTreeViewWithITems.java:25)
  at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
  at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)
  at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
  at com.sun.glass.ui.win.WinApplication$2$1.run(WinApplication.java:67)
  at java.lang.Thread.run(Thread.java:722)

示例.java:

package testtreeviewwithitems;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

public class Sample implements Initializable {

 @FXML
 private MyTestTree taskTreeView = new MyTestTree();

 @Override
 public void initialize(URL url, ResourceBundle rb) {
 } 

}

测试树视图.java:

package testtreeviewwithitems;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;



public class TestTreeView extends Application {

    public static void main(String[] args) {
    Application.launch(TestTreeView.class, args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));

        stage.setScene(new Scene(root));
        stage.show();
    }
}

MyTestTree.java:

package testtreeviewwithitems;

import javafx.scene.control.TreeView;

public class MyTestTree extends TreeView {

}

示例.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml"     fx:controller="testtreeviewwithitems.Sample">
  <children>
    <TreeView fx:id="treeView" editable="true" focusTraversable="false" layoutY="0" prefHeight="50.0" prefWidth="100.0" showRoot="false" />

  </children>
</AnchorPane>

Java版本:

java version "1.7.0_11"
Java(TM) SE Runtime Environment (build 1.7.0_11-b21)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)

更新:看来我找到了解决方案,我稍后会发布答案。

4

1 回答 1

0

首先,我使用 fxml 创建了自定义控件,然后将其包含在主 fxml 文件中,如下所示:<fx:include fx:id="treeView" source="MyTreeView.fxml"/>

于 2013-04-22T15:04:51.690 回答