Javafx GUI没有显示

时间:2015-11-28 09:58:14

标签: java user-interface javafx tableview

您好我是javaFx fxml的新学习者,我正在使用fxml测试表视图。但netbeans在我的代码中没有显示错误,我无法运行我的GUI应用程序。

我的FXMLDocument.fxml代码在这里。

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

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

        <AnchorPane id="AnchorPane" prefHeight="359.0" prefWidth="473.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="table.FXMLDocumentController">
            <children>
                <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
              <TableView fx:id="tableview" layoutX="137.0" layoutY="90.0" prefHeight="200.0" prefWidth="200.0">
                <columns>
                  <TableColumn fx:id="id" prefWidth="93.0" text="Id" />
                  <TableColumn fx:id="name" prefWidth="106.0" text="Name" />
                </columns>
              </TableView>
            </children>
        </AnchorPane>

这是一个fxml控制器代码。

package table;

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

/**
 *
 * @author pyaephyohlaing
 */
public class FXMLDocumentController implements Initializable {

    //the table's view and columns
    @FXML
    TableView<Person>tableview;
    @FXML
    TableColumn id;
    @FXML
    TableColumn name;

    //the table data
    ObservableList<Person>data = FXCollections.observableArrayList();

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

    //setup the table data
    id.setCellValueFactory(
            new PropertyValueFactory<Person,Integer>("id")
    );
    name.setCellFactory(
            new PropertyValueFactory<Person,String>("name")
    );

    data.add(new Person(1,"Mg Mg"));
    tableview.setItems(data);
    System.out.println("hello");
    }    

}

这是Person类代码。 (Person.java)

package table;
import javafx.beans.property.*;

// a class that represents the person data to add into the table view
public class Person {

//a variable that represent the column data neeed to be JavaFx properties(i.e SimpleStringProperty)   
//sets as the JavaFx table data property   
    public SimpleIntegerProperty id;
    public SimpleStringProperty name;

    public Person(Integer id,String name) {
        System.out.println("Hee");
        this.id=new SimpleIntegerProperty(id);
        this.name=new SimpleStringProperty(name);
    }

//getter method
    public Integer getId() {
        return  id.get();
    }
    public String getName() {
        return name.get();
    }

}

这是主要的Table.java代码。

package table;

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

/**
 *
 * @author pyaephyohlaing
 */
public class Table extends Application {

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

        Scene scene = new Scene(root);

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

当我运行我的项目时,GUI没有出现,它显示了很多这样的错误。

  

执行   /Users/pyaephyohlaing/NetBeansProjects/table/dist/run1649041788/table.jar   使用平台   /Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/bin/java   Application start方法中的异常   java.lang.reflect.InvocationTargetException at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:497)at   com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)     在   com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:497)at   sun.launcher.LauncherHelper $ FXHelper.main(LauncherHelper.java:767)   引起:java.lang.RuntimeException:Application start中的异常   方法   com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)     在   com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 156(LauncherImpl.java:182)     在java.lang.Thread.run(Thread.java:745)引起:   java.lang.ClassCastException:javafx.scene.control.TableColumn不能   被转换为javafx.scene.control.TableColumn $ CellDataFeatures at   javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:98)     在   com.sun.javafx.scene.control.skin.TableRowSkin.getCell(TableRowSkin.java:87)     在   com.sun.javafx.scene.control.skin.TableRowSkin.getCell(TableRowSkin.java:53)     在   com.sun.javafx.scene.control.skin.TableRowSkinBase.createCell(TableRowSkinBase.java:698)     在   com.sun.javafx.scene.control.skin.TableRowSkinBase.recreateCells(TableRowSkinBase.java:692)     在   com.sun.javafx.scene.control.skin.TableRowSkinBase.init(TableRowSkinBase.java:146)     在   com.sun.javafx.scene.control.skin.TableRowSkin。(TableRowSkin.java:64)     在javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:212)     在javafx.scene.control.Control.impl_processCSS(Control.java:859)at   javafx.scene.Node.processCSS(Node.java:9056)at   javafx.scene.Node.applyCss(Node.java:9153)at   com.sun.javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1964)     在   com.sun.javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1797)     在   com.sun.javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1879)     在   com.sun.javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2528)     在   com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1189)     在javafx.scene.Parent.layout(Parent.java:1079)at   javafx.scene.Parent.layout(Parent.java:1085)at   javafx.scene.Parent.layout(Parent.java:1085)at   javafx.scene.Scene.doLayoutPass(Scene.java:552)at   javafx.scene.Scene.preferredSize(Scene.java:1646)at   javafx.scene.Scene.impl_preferredSize(Scene.java:1720)at   javafx.stage.Window $ 9.invalidated(Window.java:846)at   javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109)     在   javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)     在javafx.stage.Window.setShowing(Window.java:922)at   javafx.stage.Window.show(Window.java:937)at   javafx.stage.Stage.show(Stage.java:259)at   table.Table.start(Table.java:27)at   com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 163(LauncherImpl.java:863)     在   com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 176(PlatformImpl.java:326)     在   com.sun.javafx.application.PlatformImpl.lambda为$ null $ 174(PlatformImpl.java:295)     在java.security.AccessController.doPrivileged(Native Method)at   com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 175(PlatformImpl.java:294)     在   com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(InvokeLaterDispatcher.java:95)   运行应用程序表的异常。表Java结果:1正在删除   目录   /用户/ pyaephyohlaing /的NetBeansProjects /表/ DIST / run1649041788   jfxsa-run:BUILD SUCCESSFUL(总时间:6秒)

我不知道我的代码有什么问题,任何人都可以帮我修复错误吗?感谢。

1 个答案:

答案 0 :(得分:1)

在FXMLDocumentController中,当我认为您要将cellValueFactory设置为name时,为Id设置单元格工厂。

变化:

id.setCellFactory(
        new PropertyValueFactory<Person,String>("name")
);

致:

name.setCellValueFactory(
        new PropertyValueFactory<Person,String>("name")
);