简体   繁体   English

javafx tableview添加一个空行数据

[英]javafx tableview is adding a null row data

I am adding a new row in tableview but it doesn't show any value and i don't know why?? 我在表视图中添加了新行,但未显示任何值,我也不知道为什么?

codes: 代码:

import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;

public class TestController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
      @FXML
    private TableView<person> table;

    @FXML
    void add(ActionEvent event) {
   ObservableList<person> data=table .getItems();

   data.add(new person("dsdsd", "sdfsdf"));
    }

    private   class person {
  private final SimpleStringProperty t1= new SimpleStringProperty("");
   private final SimpleStringProperty t2= new SimpleStringProperty("");

        public person(String t1,String t2) {
            set1Name(t1);
          set2Name(t2);
        }

           public String get1Name() {
            return t1.get();
        }

        public void set1Name(String fName) {
            t1.set(fName);
        }
          public String get2Name() {
            return t2.get();
        }

        public void set2Name(String fName) {
            t2.set(fName);
        }
    }
}

FXML XML文件

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

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

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="schoolmanagement2.TestController">
   <children>
      <TableView fx:id="table" layoutX="386.0" layoutY="79.0" prefHeight="200.0" prefWidth="200.0">
        <columns>
          <TableColumn prefWidth="75.0" text="C1">
                    <cellValueFactory>
                      <PropertyValueFactory property="t1" />
                  </cellValueFactory> 
                  </TableColumn>
          <TableColumn prefWidth="75.0" text="C2">
                    <cellValueFactory>
                      <PropertyValueFactory property="t2" />
                  </cellValueFactory> 
                  </TableColumn>
        </columns>
      </TableView>
      <Button layoutX="445.0" layoutY="310.0" mnemonicParsing="false" onAction="#add" text="Button" />
   </children>
</AnchorPane>

and i also not get any error in output window Please help me how would i add a row in table view. 而且我在输出窗口中也没有收到任何错误,请帮助我如何在表格视图中添加一行。

Thank you. 谢谢。

What's Wrong 怎么了

Your issue is the thing you are asking to reflect on, eg t1 , does not have public access methods in the person class following Java naming conventions. 您的问题是您要反映的内容,例如t1 ,在遵循Java命名约定的person类中没有公共访问方法。

So, in this case, following Java style conventions not only makes your program more readable to other developers used to reading Java, but it also makes it work with the expected behavior. 因此,在这种情况下,遵循Java样式约定不仅使您的程序对以前阅读Java的其他开发人员更具可读性,而且还使其能够按预期的方式工作。

Related Question 相关问题

This question is really a duplicate of: Javafx tableview not showing data in all columns , however I'll put an additional answer here because it deserves some commentary for defining the PropertyValueFactories in FXML. 这个问题确实是重复的: Javafx tableview并没有在所有列中显示数据 ,但是我将在此处添加一个附加答案,因为它值得一些注释,用于在FXML中定义PropertyValueFactories。

Background 背景

A PropertyValueFactory works based on reflecting on your data class using Java camel case naming conventions . PropertyValueFactory的工作原理是使用Java 驼峰式命名约定来反映您的数据类。

Do read the PropertyValueFactory javadoc I linked, it explains how it works with a sample. 请阅读我链接的PropertyValueFactory javadoc,它解释了如何与示例一起使用。

Although numeric characters are probably permitted in camel case, I haven't really seen them used much and I'm not sure how they relate (due to them being neither upper nor lower case). 尽管在驼峰式的情况下可能允许使用数字字符,但我还没有真正看到它们使用过多,也不知道它们之间的关系(由于它们既不是大写也不是小写)。 So I'd advise replacing the numeric characters in your identifiers, using the same name for the same data field everywhere it is referenced and making sure the methods required for reflection by PropertyValueFactory are publicly accessible. 因此,我建议您替换标识符中的数字字符,对引用的所有数据字段使用相同的名称,并确保PropertyValueFactory反映所需的方法可公开访问。 This will allow your example to work. 这将使您的示例有效。

Sample Code 样例代码

Here is a version of the code from your question which works. 这是您问题中有效的代码版本。

All code should be placed in a source package named formatter . 所有代码都应放在名为formatter的源包中。

样品

people.fxml people.fxml

<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="formatter.PeopleTableController">
    <children>
        <TableView fx:id="table" layoutX="386.0" layoutY="79.0" prefHeight="200.0" prefWidth="200.0">
            <columns>
                <TableColumn prefWidth="75.0" text="Given Name">
                    <cellValueFactory>
                        <PropertyValueFactory property="firstName" />
                    </cellValueFactory>
                </TableColumn>
                <TableColumn prefWidth="75.0" text="Surname">
                    <cellValueFactory>
                        <PropertyValueFactory property="surname" />
                    </cellValueFactory>
                </TableColumn>
            </columns>
        </TableView>
        <Button layoutX="445.0" layoutY="310.0" mnemonicParsing="false" onAction="#add" text="Button" />
    </children>
</AnchorPane>

PeopleApp.java PeopleApp.java

package formatter;

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

public class PeopleApp extends Application {
    @Override
    public void start(Stage stage) throws Exception{
        FXMLLoader loader = new FXMLLoader();

        Pane mainPane = loader.load(
            getClass().getResourceAsStream(
                "people.fxml"
            )
        );

        stage.setScene(
            new Scene(
                mainPane
            )
        );

        stage.show();
    }

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

PeopleTableController.java PeopleTableController.java

package formatter;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableView;

public class PeopleTableController {

    @FXML
    private TableView<Person> table;

    @FXML
    void add(ActionEvent event) {
        table.getItems().add(new Person("Bill", "Jones"));
    }
}

Person.java 人.java

package formatter;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Person {
    private final StringProperty firstName = new SimpleStringProperty("");
    private final StringProperty surname = new SimpleStringProperty("");

    public Person(String firstName, String surname) {
        setFirstName(firstName);
        setSurname(surname);
    }

    public String getFirstName() {
        return firstName.get();
    }

    public void setFirstName(String name) {
        this.firstName.set(name);
    }

    public StringProperty firstNameProperty() {
        return firstName;
    }

    public String getSurname() {
        return surname.get();
    }

    public void setSurname(String name) {
        surname.set(name);
    }

    public StringProperty surnameProperty() {
        return surname;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM