我是一名JavaFX新手,由于某些原因,我的数据并没有显示在桌面视图中。
控制器类:
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行,其他我不能,但是,对于某些行原因,内容没有显示出来。
答案 0 :(得分:0)
您在Service
中将getter和setter命名为错误。
根据camelCase命名约定命名它们:
public void setQnt(int qnt){
this.qnt = qnt;
}