【问题标题】:TableView not showing content[JavaFX]TableView 不显示内容[JavaFX]
【发布时间】:2016-09-02 18:36:51
【问题描述】:

我是 JavaFX 新手,由于某种原因,我的数据没有显示在 tableview 中。

控制器类:

public class TxstController implements Initializable {

@FXML private Label label;
@FXML private TextField searchBar;
@FXML private Button searchButton;
@FXML private ListView dateList;

@FXML private TableView<Service> serviceTable;
@FXML private TableColumn<Service, Integer> qntColumn;
@FXML private TableColumn<Service, String> descColumn;
@FXML private TableColumn<Service, Double> priceColumn;




@FXML
private void handleSearchButton(ActionEvent event) {
    //do stuff when searchButton get clicked
    List<String> lista = new ArrayList<>();
    lista.add("Queijo");
    lista.add("Azeitonas");
    lista.add("Cebola");
    fillListWith(lista);


}

@FXML
private void textFieldKeyHandler(KeyEvent event){
    if(event.getCode() == KeyCode.ENTER){         
      //do stuff when in text field you click enter
    }
}

private void fillListWith(List<String> list){
    //adds string list at param to dateList
    dateList.setItems(FXCollections.observableList(list));
}

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

 qntColumn.setCellValueFactory(new PropertyValueFactory<>("qnt"));
 descColumn.setCellValueFactory(new PropertyValueFactory<>("desc"));
 priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));


 ObservableList<Service> lista = FXCollections.observableArrayList();
 lista.add(new Service(3,"Queijo com ",19.30));
 lista.add(new Service(2,"asdasasd",5.21));
 lista.add(new Service(3,"Queijo com ",19.30));
 lista.add(new Service(3,"Queijo com ",19.30));
 lista.add(new Service(3,"Queijo com ",19.30));
 lista.add(new Service(3,"Queijo com ",19.30));

 serviceTable.setItems(lista);

}    

服务类:

class Service {

private int qnt;
private String desc;
private double price;

public Service(int qnt,String desc, double price){
    this.qnt = qnt;
    this.desc = desc;
    this.price = price;
}

public int getqnt(){
    return this.qnt;
}

public String getdesc(){
    return this.desc;
}

public double getprice(){
    return this.price;
}

public void setqnt(int qnt){
    this.qnt = qnt;
}

public void setdesc(String desc){
    this.desc = desc;
}

public void setprice(double price){
    this.price = price;
}

FXML 文件:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="758.0" prefWidth="848.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="elteste.TxstController">
    <children>
      <TextField fx:id="searchBar" layoutX="110.0" layoutY="124.0" onKeyPressed="#textFieldKeyHandler" prefHeight="26.0" prefWidth="487.0" />
      <Button fx:id="searchButton" layoutX="608.0" layoutY="124.0" mnemonicParsing="false" onAction="#handleSearchButton" prefHeight="26.0" prefWidth="84.0" text="Pesquisar" />
      <ListView fx:id="dateList" layoutX="18.0" layoutY="158.0" onMouseClicked="#listMouseSelectHandler" prefHeight="575.0" prefWidth="175.0" />
      <Label layoutX="30.0" layoutY="128.0" prefHeight="16.0" prefWidth="69.0" text="Matricula:" textAlignment="CENTER" />
      <Button layoutX="702.0" layoutY="124.0" mnemonicParsing="false" text="Adicionar Servico" />
      <Label fx:id="label" layoutX="369.0" layoutY="54.0" prefHeight="37.0" prefWidth="207.0" text="Label" />
      <TableView fx:id="serviceTable" layoutX="207.0" layoutY="158.0" prefHeight="575.0" prefWidth="603.0">
        <columns>
          <TableColumn fx:id="qntColumn" prefWidth="113.0" text="Quantidade" />
          <TableColumn fx:id="descColumn" prefWidth="362.0" text="Descricão" />
            <TableColumn fx:id="priceColumn" minWidth="0.0" prefWidth="127.0" text="Preco" />
        </columns>
      </TableView>
    </children>
</AnchorPane>

没有错误,当表格弹出时,我可以选择已填充的行,例如,如果列表有 3 个对象,我可以选择 3 行,其他我不能,但是,对于某些原因,内容不显示。

【问题讨论】:

    标签: javafx tableview


    【解决方案1】:

    您在 Service 中错误地命名了 getter 和 setter。 根据 camelCase 命名约定命名它们:

    public void setQnt(int qnt){
        this.qnt = qnt;
    }
    

    【讨论】:

    • 我已经完成了,现在出现了一些期望。
    • 它们有很多,但类似于:应用程序启动方法 java.lang.reflect.InvocationTargetException 中的异常
    • java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498)
    猜你喜欢
    • 2015-12-15
    • 2012-12-30
    • 1970-01-01
    • 2018-11-13
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2013-05-20
    相关资源
    最近更新 更多