【问题标题】:JavaFX: Strange case with DataView fill operationJavaFX:DataView 填充操作的奇怪案例
【发布时间】:2017-07-19 05:35:09
【问题描述】:

我的测试应用程序有一个非常奇怪的问题。我需要用一些数据填充 JavaFX TableView 元素。代码如下:

fxmldocumentController.java

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

import javafx.scene.control.TableView; //A
import javafx.scene.control.TableColumn; //B
import javafx.scene.control.cell.PropertyValueFactory; //C
public class fxmldocumentController implements Initializable 
{

@FXML
private TableView<employees> mainTableView;

@FXML
private TableColumn<employees, Integer> age;

@FXML
private TableColumn<employees, String> userName, companyName;

@Override
public void initialize(URL url, ResourceBundle rb) 
{
  // TODO:
  mainTableView.getItems().
add(new employees("Yuri P. Bodrov", "VMware", 35));
  mainTableView.getItems().
add(new employees("Ivan Y. Bodrov", "VMware", 5));
  mainTableView.getItems().
add(new employees("Peter Y. Bodrov", "VMware", 2));

  // A problem starts here:
  age.setCellValueFactory(new PropertyValueFactory<>("age"));
  userName.setCellValueFactory(new PropertyValueFactory<>("userName"));
  companyName.
setCellValueFactory(new PropertyValueFactory<>("companyName"));


}   

}

fxmldocument.fxml

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

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

<AnchorPane id="AnchorPane" prefHeight="300.0" prefWidth="400.0" style="-fx-
background-color: white;" 
        xmlns="http://javafx.com/javafx/8" 
xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="sampletableviewapp00.fxmldocumentController">
<children>
  <Label fx:id="testLabel" layoutX="14.0" layoutY="14.0" style="-fx-
background-color: white;" text="Employees. TableView." textFill="#505050">
     <font>
        <Font size="14.0" />
     </font>
  </Label>
  <TableView fx:id="mainTableView" layoutX="12.0" layoutY="50.0" 
prefHeight="200.0" prefWidth="377.0">
    <columns>
      <TableColumn prefWidth="90.0" text="UserName" />
      <TableColumn prefWidth="119.0" text="CompanyName" />
      <TableColumn prefWidth="84.0" text="Age" />
    </columns>

  </TableView>
</children>
</AnchorPane>

Sampletableviewapp00.java

package sampletableviewapp00;

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

public class Sampletableviewapp00 extends Application 
{

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

Scene scene = new Scene(root);

stage.setScene(scene);
stage.show();
}

public static void main(String[] args) 
{
  launch(args);
}

}

employees.java

package sampletableviewapp00;

public class employees 
{
String userName, companyName;
int age;

// Generate Properties. Getters:
public int GetAge()
{
  return age;
}

public String GetUserName()
{
  return userName;
}

public String GetCompanyName()
{
  return companyName;
}

// Generate Properties. Setters:
public void SetAge(int age)
{
  this.age = age;
}

public void SetUserName(String userName)
{
  this.userName = userName;
}

public void SetCompanyName(String companyName)
{
  this.companyName = companyName;
}

// Generate Constructor of Employees class:
public employees(String userName, String companyName, int age)
{
  this.userName = userName;
  this.companyName = companyName;
  this.age = age;
}

}

当我运行此应用程序时,NetBeans IDE 8.2 返回此异常/错误堆栈:请参阅 outputError.png 作为附件

outputError.png

outputError02.PNG

亲爱的同事们!你有什么想法来解决这个问题吗?您可以尝试自己编写此代码并运行吗?提前致谢! :-)

【问题讨论】:

  • 发布异常。
  • 我们开始吧!如图……
  • 在没有 ClassLoader 的情况下加载您的 FXML。 Parent root = FXMLLoader.load(getClass().getResource("fxmldocument.fxml"));
  • 同样的事情...codeParent root = FXMLLoader.load(getClass().getResource("fxmldocument.fxml")); :-(

标签: javafx


【解决方案1】:

您有 fxmldocument.xml 但尝试加载“fxmldocument.fxml”。 将文件重命名为具有 fxml 扩展名。

还要确保将 fxml 文件放在 /resources/yourpackagepath/ 文件夹下并加载为:

Sampletableviewapp00.class.getResource("fxmldocument.fxml")

【讨论】:

    【解决方案2】:

    这一行正在触发异常:

    Parent root = FXMLLoader.load(getClass().
    getClassLoader().getResource("fxmldocument.fxml"));
    

    您的 fxml 文档的扩展名是项目中的 xml,并且您正尝试在上一行中将其加载为 .fxml。

    fxmldocument.xml 重命名为 fxmldocument.fxml

    【讨论】:

    • 感谢各位同事的回答,但我犯了一个错误:实际上,从一开始,文件名 fxmldocument.fxml 就有一个 FXML 扩展名。看附件中的图片:outputError02.PNG
    • 亲爱的同事们!你有什么想法来解决这个问题吗?您可以尝试自己编写此代码并运行吗?提前致谢! :-)
    • 一切都很好,仔细看看文件名,也许你在小细节上搞砸了。我已经对你的问题投了赞成票,所以它会得到更多关注。
    • 成功了吗?如果您仍然可以聊天,我们一起解决问题
    • 没有。感谢您的更新! :-) 明天怎么样?你从乌萨马哪里来?其实这个问题是一个卡住了! :-(
    猜你喜欢
    • 2017-08-03
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多