我尝试替换标准TreeView时出现异常

时间:2013-04-21 07:10:00

标签: java treeview javafx

当我尝试用我的扩展版本替换标准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)

Sample.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) {
 } 

}

TestTreeView.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 {

}

Sample.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 -vesion:

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)

更新:似乎我找到了解决方案,我稍后会发布回复。

1 个答案:

答案 0 :(得分:0)

First I've created custom control using fxml然后在主要的fxml文件中包含这样的内容:<fx:include fx:id="treeView" source="MyTreeView.fxml"/>